Using two or more auth strategies in a remix app #145
-
I'm super new to this OAuth system, probably a noob amongst you guys. I wanted to use email-password and google strategy with remix-auth. Can I use them together? If I can? Can someone guide me through the using them together. 😅 Any help is appreciated ! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
You can use any number of strategies, what you need to do is to call export let authenticator = new Authenticator<User>(sessionStorage, options)
authenticator
.use(new FormStrategy(login))
.use(new GoogleStrategy(googleOptions, login))
Then, when you want to use one strategy you need to do If you want to customize the strategy name pass it as second param of authenticator.use(new FormStrategy(login), "user-pass")
authenticator.authenticate("user-pass", request) // this will use the FormStrategy One important thing, is that the function you pass to each strategy must be always return the same object, the one you pass as type to Authenticator when creating a new instance. |
Beta Was this translation helpful? Give feedback.
-
Having migrated to v4 already. Following this thread, I already have FormStrategy (session cookie) Now I want to have JWT as an alternative for authentication. My doubt is about the best to implement it. This code shows my idea. authenticator.use(
new FormStrategy(async ({ form }) => {
...
}
'form',
);
authenticator.use(
new JwtStrategy(
...
),
'jwt',
); So, should I do this for authenticating from now on? export let action: ActionFunction = async ({ request }) => {
var user = null;
try {
user = await authenticator.authenticate('form', request);
if (!user) {
user = await authenticator.authenticate('jwt', request);
}
} catch (err: any) {
// Not authenticated
}
if (user) {
// Authenticated...
} else {
// Not authenticated
}
}; @sergiodxa what do u think? Thanks a lot! |
Beta Was this translation helpful? Give feedback.
You can use any number of strategies, what you need to do is to call
authenticator.use
multiple times, one per strategy.Then, when you want to use one strategy you need to do
authenticator.authenticate("form", request)
orauthenticator.authenticate("google", request)
, that's the reason whyauthenticate
excepts the strategy name.If you want to customize the strategy name pass it as second param of
.use
like this: