Skip to content

Instantly share code, notes, and snippets.

@PatrickJS
Created November 16, 2023 01:06
Show Gist options
  • Select an option

  • Save PatrickJS/71e643b89c6c9cd30b9a1c927c6c96b0 to your computer and use it in GitHub Desktop.

Select an option

Save PatrickJS/71e643b89c6c9cd30b9a1c927c6c96b0 to your computer and use it in GitHub Desktop.
copy/paste auth.js template with callbacks
import { serverAuth$ } from '@builder.io/qwik-auth';
import type { GitHubProfile } from '@auth/core/providers/github';
import GitHub from '@auth/core/providers/github';
import type { GoogleProfile } from '@auth/core/providers/google';
import Google from '@auth/core/providers/google';
import type { Provider } from '@auth/core/providers';
export const { onRequest, useAuthSession, useAuthSignin, useAuthSignout } =
serverAuth$(({ env }) => ({
secret: env.get('AUTH_SECRET'),
trustHost: true,
debug: env.get('AUTH_DEBUG') === 'true',
providers: [
GitHub({
profile(profile: GitHubProfile) {
// console.log('\n\nGithub', profile, '\n\n');
return {
...profile,
id: profile.id.toString(),
name: profile.name ?? profile.login,
email: profile.email,
image: profile.avatar_url,
}
},
clientId: env.get('GITHUB_ID')!,
clientSecret: env.get('GITHUB_SECRET')!,
}),
Google({
clientId: env.get('GOOGLE_ID')!,
clientSecret: env.get('GOOGLE_SECRET')!,
profile(profile: GoogleProfile) {
// console.log('\n\nGoogle', profile, '\n\n');
return {
...profile,
id: profile.sub,
name: profile.name,
email: profile.email,
image: profile.picture,
}
},
}),
] as Provider[],
pages: {
// signIn: '/login',
// signOut: '/logout',
// newUser: '/signup',
},
callbacks: {
async signIn(/*{ user, account, profile, email, credentials }*/) {
// console.log('Sign in', user, account, profile, email, credentials);
// console.log('Sign in', ...arguments);
// Google also returns a email_verified boolean property in the OAuth profile.
// You can use this property to restrict access to people with verified accounts at a particular domain.
// if (account.provider === "google") {
// return profile.email_verified && profile.email.endsWith("@example.com")
// }
const isAllowedToSignIn = true;
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (isAllowedToSignIn === true) {
return true;
} else {
// Return false to display a default error message
return false;
// Or you can return a URL to redirect to:
// return '/unauthorized'
}
},
async redirect({ url, baseUrl }) {
// console.log('Redirect: \nbaseurl:', baseUrl, ' \nurl:', url);
// Allows relative callback URLs
if (url.startsWith('/')) {
const redirectUrl = `${baseUrl}${url}`
// console.log('\nRedirecting to url:', redirectUrl);
return redirectUrl;
}
else if (url === baseUrl) {
// console.log('\nRedirecting to baseUrl:', url);
return url;
}
// Allows callback URLs on the same origin
else if (new URL(url).origin === baseUrl) {
// console.log('\nRedirecting to new url:', url);
return url
}
// console.log('\nRedirecting to default:', baseUrl);
return baseUrl;
},
async session({
session
// ,user
// ,token
}) {
// console.log(`
// --Session--
// session: ${JSON.stringify(session, null, 2)}
// user: ${JSON.stringify(user, null ,2)}
// token: ${JSON.stringify(token, null, 2)}
// `);
// Send properties to the client, like an access_token from a provider.
// session.accessToken = token.accessToken
return session;
},
async jwt({ token /*, user, account, profile, isNewUser */ }) {
// console.log('JWT', token, user, account, profile, isNewUser);
// Persist the OAuth access_token to the token right after signin
// if (account) {
// token.accessToken = account.access_token
// }
return token;
},
},
}));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment