I have a Django application with authenticated (logged-in) users. UPDATE: using the built-in django authentication.
I have another (Svelte) application using Auth.js (https://authjs.dev) for authentication, currently set up with github/facebook/linkedin.
Now I want to send a user from the Django application to the Svelte application, and automagically (i) create the user in the Svelte application if they don't exist, and (ii) log them in to the Svelte application.
I want the end-user experience to be like a regular redirect from one page to the other staying logged-in in both places.
I'm stuck at the part where the user arrives in the Svelte application and I need to create a session. Does Auth.js have any way of doing this without going through a "provider"?
What I have so far is (routes/api/sso/+server.ts):
import { redirect } from '@sveltejs/kit'
export async function GET({url, locals, cookies}) { // should perhaps be POST..?
// TODO: validate request..
// where to go next?
const next = url.searchParams.get('next')
if (!next) throw new Error("next is required")
// who are we logging in?
const usr = get_or_create(url.searchParams('username')) // and name/email/image/etc.
// mimic auth.js login behavior...
const session_token = create_new_session_token()
locals.session = {
id: session_token,
user: {name: usr.name, email: usr.email, image: usr.image}
expires: new Date(Date.now() + 1000 * 60 * 60 * 2) // two hours
}
locals.user = usr
// ..this might be the only line needed (I'm guessing auth.js will set locals.{session,user} next time it receives a request)?
cookies.set("authjs.session-token", session_token, {path: "/", httpOnly: true, secure: true, sameSite: "strict"})
return redirect(307, next)
}