File tree 10 files changed +106
-8
lines changed
10 files changed +106
-8
lines changed Original file line number Diff line number Diff line change @@ -8,5 +8,8 @@ DATABASE_URL="postgresql://postgres:example@localhost:5432/postgres"
8
8
GITHUB_CLIENT_ID = " 63b03dd1a029181eede8"
9
9
GITHUB_CLIENT_SECRET = " fe61fec0056b571d01d0a1e25945904237d164e8"
10
10
11
+ # to generate, run
12
+ # openssl rand -base64 172 | tr -d '\n'
13
+ # in your terminal
11
14
NEXTAUTH_SECRET = " my_ultra_secure_nextauth_secret"
12
15
NEXTAUTH_URL = " http://localhost:3000"
Original file line number Diff line number Diff line change @@ -3,6 +3,9 @@ const nextConfig = {
3
3
images : {
4
4
domains : [ "avatars.githubusercontent.com" ] ,
5
5
} ,
6
+ experimental : {
7
+ serverActions : true ,
8
+ } ,
6
9
} ;
7
10
8
11
module . exports = nextConfig ;
Original file line number Diff line number Diff line change @@ -16,6 +16,7 @@ model User {
16
16
image String ?
17
17
accounts Account []
18
18
sessions Session []
19
+ results Result []
19
20
}
20
21
21
22
// Necessary for Next auth
@@ -52,3 +53,10 @@ model VerificationToken {
52
53
53
54
@@unique ([identifier , token ] )
54
55
}
56
+
57
+ model Result {
58
+ id String @id @default (cuid () )
59
+ takenTime String
60
+ userId String
61
+ user User @relation (fields : [userId ] , references : [id ] , onDelete : Cascade )
62
+ }
Original file line number Diff line number Diff line change
1
+ "use server" ;
2
+
3
+ import { User } from "@prisma/client" ;
4
+ import { prisma } from "@/lib/prisma" ;
5
+
6
+ export async function saveUserResult ( input : {
7
+ userId : User [ "id" ] ;
8
+ timeTaken : string | number ;
9
+ } ) {
10
+ if ( typeof input . timeTaken !== "string" && typeof input . timeTaken !== "number" ) {
11
+ throw new Error ( "Invalid input." ) ;
12
+ }
13
+
14
+ await prisma . result . create ( {
15
+ data : {
16
+ userId : input . userId ,
17
+ takenTime : input . timeTaken . toString ( ) ,
18
+ } ,
19
+ } ) ;
20
+ }
Original file line number Diff line number Diff line change @@ -13,21 +13,49 @@ declare module "next-auth" {
13
13
14
14
export const nextAuthOptions = {
15
15
adapter : PrismaAdapter ( prisma ) ,
16
+ session : {
17
+ strategy : 'jwt' ,
18
+ } ,
19
+ secret : process . env . NEXTAUTH_SECRET ! ,
16
20
providers : [
17
21
GithubProvider ( {
18
22
clientId : process . env . GITHUB_CLIENT_ID ! ,
19
23
clientSecret : process . env . GITHUB_CLIENT_SECRET ! ,
20
24
} ) ,
21
25
] ,
22
26
callbacks : {
23
- session ( { session, user } ) {
24
- if ( session . user ) {
25
- session . user . id = user . id ;
27
+ async jwt ( { token, user } ) {
28
+ const dbUser = await prisma . user . findFirst ( {
29
+ where : {
30
+ email : token . email ,
31
+ } ,
32
+ } ) ;
33
+
34
+ if ( ! dbUser ) {
35
+ if ( user ) {
36
+ token . id = user . id ;
37
+ }
38
+ return token ;
39
+ }
40
+
41
+ return {
42
+ id : dbUser . id ,
43
+ name : dbUser . name ,
44
+ email : dbUser . email ,
45
+ picture : dbUser . image ,
46
+ } ;
47
+ } ,
48
+ async session ( { token, session } ) {
49
+ if ( token ) {
50
+ session . user . id = token . id ;
51
+ session . user . name = token . name ;
52
+ session . user . email = token . email ;
53
+ session . user . image = token . picture ;
26
54
}
27
55
28
56
return session ;
29
- }
30
- }
57
+ } ,
58
+ } ,
31
59
} satisfies AuthOptions ;
32
60
33
61
const handler = NextAuth ( nextAuthOptions ) ;
Original file line number Diff line number Diff line change 1
1
import Header from "@/components/header" ;
2
2
import { Button } from "@/components/ui/button" ;
3
3
import { LoginButton , LogoutButton } from "@/components/ui/buttons" ;
4
- import { getSession } from "@/lib/getSession " ;
4
+ import { getSession } from "@/lib/session " ;
5
5
import { prisma } from "@/lib/prisma" ;
6
6
import Image from "next/image" ;
7
7
Original file line number Diff line number Diff line change 1
1
import TypingCode from "@/components/typing/typingCode" ;
2
+ import { getCurrentUser } from "@/lib/session" ;
2
3
3
4
export default async function Race ( ) {
5
+ const user = await getCurrentUser ( ) ;
6
+
7
+ console . log ( user ) ;
8
+
4
9
return (
5
10
< >
6
11
< main className = "flex min-h-screen flex-col items-center justify-between p-24" >
7
- < TypingCode />
12
+ < TypingCode user = { user } />
8
13
</ main >
9
14
</ >
10
15
) ;
Original file line number Diff line number Diff line change 3
3
import { useState , useEffect } from "react" ;
4
4
5
5
import DisplayedCode from "./displayedCode" ;
6
+ import type { User } from "next-auth" ;
7
+ import { saveUserResult } from "@/app/_actions/result" ;
6
8
import { Input } from "@/components/ui/input" ;
7
9
import { Button } from "@/components/ui/button" ;
8
10
9
11
const code = `printf("hello world")` ;
10
12
11
- export default function TypingCode ( ) {
13
+ interface TypingCodeProps {
14
+ user ?: User ;
15
+ }
16
+
17
+ export default function TypingCode ( { user } : TypingCodeProps ) {
12
18
const [ input , setInput ] = useState ( "" ) ;
13
19
const [ startTime , setStartTime ] = useState < Date | null > ( null ) ;
14
20
const [ endTime , setEndTime ] = useState < Date | null > ( null ) ;
@@ -20,6 +26,8 @@ export default function TypingCode() {
20
26
const timeTaken : number =
21
27
( endTime . getTime ( ) - startTime . getTime ( ) ) / 1000 ;
22
28
29
+ if ( user ) saveUserResult ( { userId : user . id , timeTaken } ) ;
30
+
23
31
console . log ( "Time taken:" , timeTaken ) ;
24
32
}
25
33
} , [ endTime , startTime ] ) ;
Original file line number Diff line number Diff line change @@ -4,3 +4,9 @@ import { getServerSession } from "next-auth";
4
4
export function getSession ( ) {
5
5
return getServerSession ( nextAuthOptions ) ;
6
6
}
7
+
8
+ export async function getCurrentUser ( ) {
9
+ const session = await getSession ( ) ;
10
+
11
+ return session ?. user ;
12
+ }
Original file line number Diff line number Diff line change
1
+ import type { User } from "next-auth" ;
2
+
3
+ type UserId = string ;
4
+
5
+ declare module "next-auth/jwt" {
6
+ interface JWT {
7
+ id : UserId ;
8
+ }
9
+ }
10
+
11
+ declare module "next-auth" {
12
+ interface Session {
13
+ user : User & {
14
+ id : UserId ;
15
+ } ;
16
+ }
17
+ }
You can’t perform that action at this time.
0 commit comments