Skip to content

Commit

Permalink
feat(session): add email_verified in session's user
Browse files Browse the repository at this point in the history
  • Loading branch information
adrienZ committed Oct 3, 2024
1 parent 94b9237 commit 871cdb8
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
11 changes: 10 additions & 1 deletion playground/server/routes/auth/github.get.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { drizzle as drizzleIntegration } from "db0/integrations/drizzle/index";

export default defineOAuthGitHubEventHandler({
config: {
emailRequired: true,
},
async onSuccess(event, { user }) {
const auth = useSlipAuth();
const db = drizzleIntegration(useDatabase());

const [userId, sessionFromDb] = await auth.OAuthLoginUser({
email: user.email,
Expand All @@ -13,14 +16,20 @@ export default defineOAuthGitHubEventHandler({
ip: getRequestIP(event),
});

const userDb = await db
.select()
.from(auth.schemas.users)
.get();

await setUserSession(event, {
expires_at: sessionFromDb.expires_at,
id: sessionFromDb.id,
user: {
id: userId,
email_verified: userDb?.email_verified || false,
},
});
return sendRedirect(event, "/");
return sendRedirect(event, "/profile");
},
// Optional, will return a json error and 401 status code by default
onError(event, error) {
Expand Down
5 changes: 2 additions & 3 deletions src/runtime/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,11 @@ declare module "nuxt/schema" {

export type SlipAuthPublicSession = Pick<SlipAuthSession, "id" | "expires_at">;

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore #auth-utils is an alias from nuxt-auth-utils
// @ts-expect-error #auth-utils is an alias from nuxt-auth-utils
declare module "#auth-utils" {

// eslint-disable-next-line @typescript-eslint/no-empty-object-type
interface UserSession extends SlipAuthPublicSession {}
// eslint-disable-next-line @typescript-eslint/no-empty-object-type
interface User extends Pick<SlipAuthUser, "id"> {}
interface User extends Pick<SlipAuthUser, "id" | "email_verified"> {}
}

0 comments on commit 871cdb8

Please sign in to comment.