-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #119 from siinghd/session
FEAT: single session/login
- Loading branch information
Showing
11 changed files
with
198 additions
and
3 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 |
---|---|---|
|
@@ -14,3 +14,4 @@ BOT_TOKEN = "123" | |
GUILD_ID = "123" | ||
LOCAL_CMS_PROVIDER = true | ||
CACHE_EXPIRE_S = 10 | ||
|
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,27 @@ | ||
'use server'; | ||
import db from '@/db'; | ||
|
||
export const logoutUser = async (email: string, adminPassword: string) => { | ||
if (adminPassword !== process.env.ADMIN_SECRET) { | ||
return { error: 'Unauthorized' }; | ||
} | ||
|
||
const user = await db.user.findFirst({ | ||
where: { | ||
email, | ||
}, | ||
}); | ||
if (!user) { | ||
return { message: 'User not found' }; | ||
} | ||
await db.user.update({ | ||
where: { | ||
id: user.id, | ||
}, | ||
data: { | ||
token: '', | ||
}, | ||
}); | ||
|
||
return { message: 'User logged out' }; | ||
}; |
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,63 @@ | ||
'use client'; | ||
import { logoutUser } from '@/actions/user'; | ||
import { Button } from '@/components/ui/button'; | ||
import { Input } from '@/components/ui/input'; | ||
import { Label } from '@radix-ui/react-dropdown-menu'; | ||
import React from 'react'; | ||
import { toast } from 'sonner'; | ||
|
||
const LogoutUserComp = () => { | ||
const formRef = React.useRef<HTMLFormElement>(null); | ||
|
||
const handlLogout = async (e: React.FormEvent<HTMLFormElement>) => { | ||
e.preventDefault(); | ||
const formData = new FormData(e.target as HTMLFormElement); | ||
|
||
const email = formData.get('email') as string; | ||
const adminPassword = formData.get('adminPassword') as string; | ||
const res = await logoutUser(email, adminPassword); | ||
toast.info(res.message); | ||
}; | ||
return ( | ||
<form onSubmit={handlLogout} ref={formRef}> | ||
<div className="w-full max-w-sm rounded-lg border border-gray-200 grid grid-rows-5 shadow-sm dark:border-gray-800"> | ||
<div className="p-6 grid gap-2 items-center row-span-3"> | ||
<div className="text-3xl font-bold">Logout the user</div> | ||
<div className="text-sm font-medium leading-none text-gray-500 dark:text-gray-400"> | ||
Enter the information below to logout the user | ||
</div> | ||
</div> | ||
|
||
<div className="p-6 flex items-center row-span-2"> | ||
<Label className="sr-only">EMAIL</Label> | ||
<Input | ||
className="w-full" | ||
id="email" | ||
name="email" | ||
placeholder="[email protected]" | ||
style={{ | ||
minWidth: '0', | ||
}} | ||
/> | ||
</div> | ||
<div className="p-6 flex items-center row-span-2"> | ||
<Label className="sr-only">Admin password</Label> | ||
<Input | ||
className="w-full" | ||
id="adminPassword" | ||
name="adminPassword" | ||
placeholder="Admin password" | ||
style={{ | ||
minWidth: '0', | ||
}} | ||
/> | ||
</div> | ||
<div className="p-6 flex items-center justify-center row-span-2"> | ||
<Button className="w-full">Logout</Button> | ||
</div> | ||
</div> | ||
</form> | ||
); | ||
}; | ||
|
||
export default LogoutUserComp; |
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,12 @@ | ||
import React from 'react'; | ||
import LogoutUserComp from './LogoutUser'; | ||
|
||
const UserAdminPage = () => { | ||
return ( | ||
<div className="flex justify-center h-[100dvh] items-center"> | ||
<LogoutUserComp /> | ||
</div> | ||
); | ||
}; | ||
|
||
export default UserAdminPage; |
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,15 @@ | ||
import { type NextRequest, NextResponse } from 'next/server'; | ||
import db from '@/db'; | ||
|
||
export async function GET(req: NextRequest) { | ||
const url = new URL(req.url); | ||
const token = url.searchParams.get('token'); | ||
const user = await db.user.findFirst({ | ||
where: { | ||
token, | ||
}, | ||
}); | ||
return NextResponse.json({ | ||
user, | ||
}); | ||
} |
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 client'; | ||
|
||
import { signOut } from 'next-auth/react'; | ||
import React, { useEffect } from 'react'; | ||
import { Toaster } from '@/components/ui/sonner'; | ||
import { toast } from 'sonner'; | ||
|
||
const page = () => { | ||
useEffect(() => { | ||
signOut({ | ||
callbackUrl: '/signin', | ||
}); | ||
toast('Too many devices connected. Logging out!', { | ||
action: { | ||
label: 'Close', | ||
onClick: () => console.log('Closed Toast'), | ||
}, | ||
}); | ||
}, []); | ||
|
||
return ( | ||
<div> | ||
<Toaster /> | ||
</div> | ||
); | ||
}; | ||
|
||
export default page; |
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,21 @@ | ||
import { withAuth } from 'next-auth/middleware'; | ||
import { NextResponse } from 'next/server'; | ||
|
||
export const config = { | ||
matcher: ['/courses/:path*'], | ||
}; | ||
|
||
export default withAuth(async (req) => { | ||
const token = req.nextauth.token; | ||
if (!token) { | ||
return NextResponse.redirect(new URL('/invalidsession', req.url)); | ||
} | ||
const user = await fetch( | ||
`${process.env.NEXT_PUBLIC_BASE_URL_LOCAL}/api/user?token=${token.jwtToken}`, | ||
); | ||
|
||
const json = await user.json(); | ||
if (!json.user) { | ||
return NextResponse.redirect(new URL('/invalidsession', req.url)); | ||
} | ||
}); |