Recommended way to use the access token in the client. #40
-
Hi, |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
This functionality was addressed in #37. The session is accessible to the user on the client-side. So, you can wrap your own session using the You may also store refresh tokens in the SKA token as explained in #28 to later rotate access tokens. For that use the export const appAuth = new SvelteKitAuth({
providers: [
new GoogleOAuthProvider({
clientId: import.meta.env.VITE_GOOGLE_OAUTH_CLIENT_ID,
clientSecret: import.meta.env.VITE_GOOGLE_OAUTH_CLIENT_SECRET,
profile(profile, tokens) {
// include tokens and a provider ID in the profile, which is then stored in token.user by SKA
return { ...profile, ...tokens, provider: "google" };
},
}),
],
callbacks: {
session (token, session) {
if (token.user) {
// avoid exposing refresh tokens to the client for security purposes
const { refresh_token, ...user } = token.user;
return { user };
}
},
jwt (token, profile) {
if (token.accessTokenExpired) {
// refresh access token
token = { ...token, access_token: newAccessToken };
}
return token;
},
},
}); This flow adds all the security measures and stability to sessions you will need. In SvelteKitAuth the user first logs in, which is handled through the providers, and the base After the profile has been returned, SKA will store that in One possible issue might be with short-lived access tokens from your auth provider, that they will expire while your client is still browsing the application. For that there is currently no solution, so you will have to manually run a fetch on fetch("/api/auth/session").then(session.set); This will force your |
Beta Was this translation helpful? Give feedback.
This functionality was addressed in #37. The session is accessible to the user on the client-side. So, you can wrap your own session using the
access_token
passed by the provider with theprofile()
callback if you're using the defaultOAuth2Provider
, and then, pass that on to your client-side session with thesession()
callback.You may also store refresh tokens in the SKA token as explained in #28 to later rotate access tokens. For that use the
profile()
callback in your provider and return the tokens along with the profile, and then if necessary refresh the access token using the refresh token in thejwt()
callback: