-
-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
34 changed files
with
1,276 additions
and
154 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
import './src/env.mjs'; | ||
import './src/env.js'; | ||
|
||
/** @type {import('next').NextConfig} */ | ||
const nextConfig = { | ||
transpilePackages: ['@orbitkit/db'], | ||
transpilePackages: ['@orbitkit/db', '@orbitkit/auth'], | ||
}; | ||
|
||
export default nextConfig; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { validateGithubCallback } from '@orbitkit/auth/providers/github'; | ||
|
||
export { validateGithubCallback as GET }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { createGithubAuthorizationURL } from '@orbitkit/auth/providers/github'; | ||
|
||
export { createGithubAuthorizationURL as GET }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import Link from 'next/link'; | ||
|
||
export default function Page() { | ||
return ( | ||
<> | ||
<h1>Sign in</h1> | ||
<Link href="/login/github">Sign in with Github</Link> | ||
</> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
/** @type {import('eslint').Linter.Config} */ | ||
const config = { | ||
root: true, | ||
extends: ['orbitkit/base'], | ||
}; | ||
|
||
module.exports = config; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
{ | ||
"name": "@orbitkit/auth", | ||
"version": "0.1.0", | ||
"private": true, | ||
"description": "Auth package for OrbitKit", | ||
"license": "MIT", | ||
"author": "OrbitKit", | ||
"sideEffects": false, | ||
"type": "module", | ||
"exports": { | ||
".": "./src/index.ts", | ||
"./env": "./src/env.js", | ||
"./providers/github": "./src/providers/github.ts", | ||
"./actions/logout": "./src/actions/logout.ts" | ||
}, | ||
"scripts": { | ||
"lint": "eslint . --cache --max-warnings 0", | ||
"typecheck": "tsc --noEmit --tsBuildInfoFile .tsbuildinfo" | ||
}, | ||
"dependencies": { | ||
"@lucia-auth/adapter-drizzle": "^1.0.0", | ||
"@orbitkit/db": "workspace:^", | ||
"@t3-oss/env-core": "^0.8.0", | ||
"@types/react-dom": "^18.2.18", | ||
"arctic": "^1.1.0", | ||
"lucia": "^3.0.1", | ||
"next": "14.1.0", | ||
"oslo": "^1.0.3", | ||
"react": "^18.2.0", | ||
"react-dom": "^18.2.0", | ||
"zod": "^3.22.4" | ||
}, | ||
"devDependencies": { | ||
"@orbitkit/tsconfig": "workspace:^", | ||
"@types/node": "^20.11.10", | ||
"@types/react": "^18.2.48", | ||
"eslint-config-orbitkit": "workspace:^" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
'use server'; | ||
|
||
import { cookies } from 'next/headers'; | ||
import { redirect } from 'next/navigation'; | ||
|
||
import { getSession } from '../getSession'; | ||
import { lucia } from '../lucia'; | ||
|
||
export async function logout() { | ||
const { session } = await getSession(); | ||
if (!session) { | ||
return { | ||
error: 'Unauthorized', | ||
}; | ||
} | ||
|
||
await lucia.invalidateSession(session.id); | ||
|
||
const sessionCookie = lucia.createBlankSessionCookie(); | ||
|
||
cookies().set( | ||
sessionCookie.name, | ||
sessionCookie.value, | ||
sessionCookie.attributes, | ||
); | ||
|
||
return redirect('/login'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { createEnv } from '@t3-oss/env-core'; | ||
import { vercel } from '@t3-oss/env-core/presets'; | ||
import { z } from 'zod'; | ||
|
||
export const env = createEnv({ | ||
extends: [vercel], | ||
server: { | ||
NODE_ENV: z.enum(['development', 'test', 'production']).optional(), | ||
GITHUB_CLIENT_ID: z.string(), | ||
GITHUB_CLIENT_SECRET: z.string(), | ||
}, | ||
runtimeEnv: { | ||
NODE_ENV: process.env['NODE_ENV'], | ||
GITHUB_CLIENT_ID: process.env['GITHUB_CLIENT_ID'], | ||
GITHUB_CLIENT_SECRET: process.env['GITHUB_CLIENT_SECRET'], | ||
}, | ||
emptyStringAsUndefined: true, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { cache } from 'react'; | ||
import { cookies } from 'next/headers'; | ||
|
||
import { type Session, type User } from 'lucia'; | ||
|
||
import { lucia } from './lucia'; | ||
|
||
export const getSession = cache( | ||
async (): Promise< | ||
{ user: User; session: Session } | { user: null; session: null } | ||
> => { | ||
const sessionId = cookies().get(lucia.sessionCookieName)?.value ?? null; | ||
if (!sessionId) { | ||
return { | ||
user: null, | ||
session: null, | ||
}; | ||
} | ||
|
||
const result = await lucia.validateSession(sessionId); | ||
try { | ||
if (result.session && result.session.fresh) { | ||
const sessionCookie = lucia.createSessionCookie(result.session.id); | ||
cookies().set( | ||
sessionCookie.name, | ||
sessionCookie.value, | ||
sessionCookie.attributes, | ||
); | ||
} | ||
if (!result.session) { | ||
const sessionCookie = lucia.createBlankSessionCookie(); | ||
cookies().set( | ||
sessionCookie.name, | ||
sessionCookie.value, | ||
sessionCookie.attributes, | ||
); | ||
} | ||
} catch {} | ||
return result; | ||
}, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './lucia'; | ||
export * from './getSession'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { DrizzlePostgreSQLAdapter } from '@lucia-auth/adapter-drizzle'; | ||
import { Lucia } from 'lucia'; | ||
|
||
import { db } from '@orbitkit/db'; | ||
import { sessionTable, userTable } from '@orbitkit/db/schema'; | ||
|
||
import { env } from './env.js'; | ||
|
||
const adapter = new DrizzlePostgreSQLAdapter(db, sessionTable, userTable); | ||
|
||
export const lucia = new Lucia(adapter, { | ||
sessionCookie: { | ||
attributes: { | ||
secure: env.NODE_ENV === 'production', | ||
}, | ||
}, | ||
getUserAttributes: (attributes) => { | ||
return { | ||
githubId: attributes.github_id, | ||
username: attributes.username, | ||
}; | ||
}, | ||
}); | ||
|
||
declare module 'lucia' { | ||
interface Register { | ||
Lucia: typeof lucia; | ||
DatabaseUserAttributes: { | ||
github_id: string; | ||
username: string; | ||
}; | ||
} | ||
} |
Oops, something went wrong.