-
Hey, so i'm having issues with understanding how I should login the user properly with pocketbase oauth, currently I have it setup correctly and it does auth the user temporarily but only on the oauth route. +server.js (oauth) import { redirect } from '@sveltejs/kit';
export const GET = async ({ locals, url, cookies }) => {
const redirectURL = `${url.origin}/auth/oauth`;
const expectedState = cookies.get('state');
const expectedVerifier = cookies.get('verifier');
const state = url.searchParams.get('state');
const code = url.searchParams.get('code');
if (expectedState !== state) {
console.log('Returned State Does not Match Expected', expectedState, state);
throw redirect(303, '/auth/login');
}
try {
const authMethods = await locals.pb?.collection('users').listAuthMethods();
const provider = authMethods.authProviders.find((provider) => provider.name === 'google');
const auth = await locals.pb?.collection('users').authWithOAuth2Code(provider.name, code, expectedVerifier, redirectURL);
console.log(provider);
console.log(auth);
} catch (err) {
console.log('Error logging in with OAuth2 user', err);
throw redirect(303, '/auth/login');
}
throw redirect(303, '/');
}; +page.server.js (login page route) import { redirect } from '@sveltejs/kit';
import { serializeNonPOJOs } from '$lib/utils';
export const actions = {
login: async ({ request, locals }) => {
const body = Object.fromEntries(await request.formData());
try {
await locals.pb.collection('users').authWithPassword(body.email, body.password);
if (!locals.pb?.authStore?.model?.verified) {
locals.pb.authStore.clear();
return {
success: false,
notVerified: true
};
}
} catch (err) {
if (err?.response?.code === 400) {
return {
success: false,
error: 'Invalid email or password'
};
}
console.error('Unexpected error:', err);
return {
success: false,
error: 'An unexpected error occurred. Please try again later.'
};
}
throw redirect(303, '/');
},
OAuth2: async ({ cookies, url, locals }) => {
const authMethods = await locals.pb?.collection('users').listAuthMethods();
if (!authMethods) {
return {
authProviderRedirect: '',
authProviderState: ''
};
}
const redirectURL = `${url.origin}/auth/oauth`;
const googleAuthProvider = authMethods.authProviders[0];
const authProviderRedirect = `${googleAuthProvider.authUrl}${redirectURL}`;
const provider = (await locals.pb?.collection('users').listAuthMethods()).authProviders.find((provider) => provider.name === 'google');
const state = googleAuthProvider.state;
const verifier = googleAuthProvider.codeVerifier;
cookies.set('provider', provider, { path: '/' });
cookies.set('state', state, { path: '/' });
cookies.set('verifier', verifier, { path: '/' });
throw redirect(302, authProviderRedirect);
}
}
export const load = async ({ locals }) => {
if (locals.user) {
throw redirect(303, '/');
}
}; |
Beta Was this translation helpful? Give feedback.
Answered by
bergethon
May 26, 2024
Replies: 1 comment 1 reply
-
I would suggest looking at this implementation, it uses oauth and username+pass login: |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
xrage201
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I would suggest looking at this implementation, it uses oauth and username+pass login:
https://github.com/kilroyjones/sveltekit-pocketbase