-
I have everything setup using form strategy as a temporary strategy to authenticate. on my login page I have this loader function: export async function action({ request }: ActionFunctionArgs) {
const user = await authenticator.authenticate(FORM_STRATEGY, request);
if (!user) return redirect("/login");
const cookie = user.get("set-cookie") ?? "";
const [accessToken] = cookie.split(";");
const [key] = accessToken.split("=");
const session = await getSession(user.get("Cookie"));
session.set(key, accessToken);
return redirect("/", {
headers: {
"Set-Cookie": await commitSession(session),
},
});
} And then on my protected pages, I have a loader function that will redirect me back to login page if I am not authenticated however when I added this piece of code the export const loader: LoaderFunction = async ({ request }: LoaderFunctionArgs) => {
const user = await authenticator.isAuthenticated(request, {
failureRedirect: "/login",
});
console.log("Protected user:", user); <--- always null
return user;
}; I use |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
If you don't get anything from isAuthenticated it can means the cookie was not sent by the browser (e.g. you set secure: true and you're not using http or the cookie expired). Another issue could be that the browser sent the cookie, but the user data is not in the correct session key, by default the Authenticator class expects the session key to be user, so when you do For convenience, the Authenticator instance has an let authenticator = new Authenticator(sessionStorage, { sessionKey: "custom" }) And of course if you customize it then |
Beta Was this translation helpful? Give feedback.
-
I have the cookie set to secure true and only https, maybe that's the reason why it's empty meaning I can't access it on the client or browser side. However I tried using the cookie helper from Remix and was able to get the cookie details or using |
Beta Was this translation helpful? Give feedback.
-
Finally I now know where the issue lies, so not only on the Thanks I think we can now close this discussion! |
Beta Was this translation helpful? Give feedback.
The user data can be anything, I store a token for example, but ensure that you're using the
authenticator.sessionKey
, otherwiseauthenticator.isAuthenticated
will not be able to find the user data.The Session is basically an object, and if you do
session.set("key1", "data")
that will mean your session is now thisIf you then try to do
session.get("key2")
it won't finddata
because it's a different key, if you don't useauthenticator.sessionKey
to save the user data (your string) then the next time you callauthenticator.isAuthenticated
it will try to use a different key and not find your data.