Sleighty
11/12/2021, 11:01 AMegnus
11/12/2021, 12:49 PMMrPoule
11/12/2021, 12:52 PMegnus
11/12/2021, 12:53 PMMrPoule
11/12/2021, 12:54 PMegnus
11/12/2021, 12:58 PM/api/signup
and then I do not return the supabase cookie back, I try to keep the FE agnostic to any BE resource so it becames easy for me to detach services whenever we have to scale.egnus
11/12/2021, 1:00 PMegnus
11/12/2021, 1:01 PMuserId
the role
the current organization
and many more metadata. Things that I need constantly and I want to avoid calling supabase for the basics that I know already and I can trustMrPoule
11/12/2021, 1:03 PMegnus
11/12/2021, 1:03 PMuser
related data without parameters, and auth.user()
will be empty. Also RLS
will not work so it is better to disable it or use the service_auth from the server and do yourself the securities that you expect to haveMihai Andrei
11/13/2021, 10:35 PMegnus
11/14/2021, 3:44 PMpost
endpoint for sign-in with sveltekit
which is the simplest.
javascript
export const post: RequestHandler<Locals, postInput, postOutput> = async (req) => {
if (req.locals.userId) {
return {
status: 403,
body: { success: false, error: 'User already signed in' },
};
}
if (!req.body) {
return {
status: 400,
body: {
success: false,
error: 'No body',
},
};
}
const { email, password } = req.body;
if (!email || !password) {
return {
status: 400,
body: {
success: false,
error: 'Missing email or password',
},
};
}
// This FN dinamically imports supabase dependency (for better SSR speed), creates a supabase instance and calls supabase.auth.signIn
const { user, error } = await signIn({ email, password });
if (error) {
return {
status: error.status,
body: {
success: false,
error: error.message,
},
};
}
if (user) {
const userId = user.id;
req.locals.userId = userId;
// We create a custom token cookie with JWT that is unnaccesible by JS and can be trusted in following requests
const userCookie = createUserCookie(JWTSign(userId));
return {
status: 200,
headers: {
'Set-Cookie': userCookie,
},
body: {
success: true,
// this is a SvelteKit action, you can ignore it for other SSR providers
session: getSession(req) as Session,
},
};
}
return {
status: 500,
body: {
success: false,
error: 'Unknown error',
},
};
};
Hope it helpsMihai Andrei
11/14/2021, 11:08 PM