-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
global session management throughout the website, User Login function…
…ality with secure OTP-based email verification, includes an .env.example (#375) * initialised next app and added homepage * setup email, prisma, DB schema, nextAUTH * fic * conflict * user signup frontend and backend * auth functionality complete * added .env.example
- Loading branch information
1 parent
4e3433d
commit 39ca948
Showing
12 changed files
with
302 additions
and
96 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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
DATABASE_URL="yourMongoDBurl/scruter" | ||
|
||
SMTP_HOST=smtp.gmail.com | ||
SMTP_PORT=587 | ||
SMTP_SECURE=false | ||
SMTP_USER=email app id | ||
SMTP_PASS=email app PW | ||
|
||
NEXTAUTH_URL=http://localhost:3000 or prod url | ||
NEXTAUTH_SECRET=password_nextauth for signing JWT tokens |
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,33 @@ | ||
"use server" | ||
import { generateAndSendOTP } from '@/lib/auth'; | ||
import prismadb from '@/lib/prismadb'; | ||
import { Prisma, User } from '@prisma/client'; | ||
|
||
export async function UserVerify({ | ||
}:{ | ||
email:string | ||
}): Promise<{ success: boolean; error?: string ; data?:User}> { | ||
|
||
const exitingUser = await prismadb.user.findUnique({ | ||
where: { | ||
email: email, | ||
}, | ||
}); | ||
|
||
if (!exitingUser) { | ||
return { | ||
success: false, | ||
error: 'User does not exists', | ||
}; | ||
} | ||
const resp=await generateAndSendOTP(email,"user"); | ||
|
||
if(!resp){ | ||
return {success:false, error:"Error occured in sending otp"}; | ||
} | ||
|
||
return { success: 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
94 changes: 94 additions & 0 deletions
94
scruter-nextjs/app/(routes)/auth/components/user/login-form.tsx
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,94 @@ | ||
'use client'; | ||
|
||
import * as React from 'react'; | ||
|
||
import { cn } from '@/lib/utils'; | ||
import { Icons } from '@/components/ui/icons'; | ||
import { Label } from '@/components/ui/label'; | ||
import { Input } from '@/components/ui/input'; | ||
import { Button } from '@/components/ui/button'; | ||
import toast, { Toaster } from 'react-hot-toast'; | ||
import { OtpForm } from '../otp-form'; | ||
import { UserVerify } from '@/actions/user/login-action'; | ||
|
||
interface UserAuthFormProps extends React.HTMLAttributes<HTMLDivElement> { | ||
authType: 'signup' | 'login'; | ||
} | ||
|
||
export function UserLoginForm({ | ||
className, | ||
authType, | ||
...props | ||
}: UserAuthFormProps) { | ||
const [isLoading, setIsLoading] = React.useState<boolean>(false); | ||
const [email, setEmail] = React.useState(''); | ||
const [otpOpen, setOtpOpen] = React.useState(false); | ||
|
||
async function onSubmit(event: React.SyntheticEvent) { | ||
event.preventDefault(); | ||
setIsLoading(true); | ||
|
||
if (!email) { | ||
toast.error('missing details'); | ||
setIsLoading(false) | ||
return; | ||
} | ||
const res = await UserVerify({ | ||
email: email, | ||
}); | ||
|
||
if (!res.success && res.error) { | ||
toast.error(res.error); | ||
setIsLoading(false) | ||
return; | ||
} | ||
|
||
toast.success('user is valid, please enter OTP'); | ||
|
||
setOtpOpen(true); | ||
setIsLoading(false); | ||
} | ||
|
||
return ( | ||
<div className={cn('grid gap-6', className)} {...props}> | ||
<Toaster/> | ||
{!otpOpen && ( | ||
<form onSubmit={onSubmit}> | ||
<div className="grid gap-2"> | ||
<div className="grid gap-1"> | ||
<Label className="sr-only" htmlFor="email"> | ||
</Label> | ||
<Input | ||
id="email" | ||
placeholder="[email protected]" | ||
type="email" | ||
autoCapitalize="none" | ||
autoComplete="email" | ||
autoCorrect="off" | ||
disabled={isLoading} | ||
value={email} | ||
onChange={e => { | ||
setEmail(e.target.value); | ||
}} | ||
/> | ||
</div> | ||
<Button type="submit" className='bg-black' disabled={isLoading}> | ||
{isLoading && ( | ||
<Icons.spinner className="mr-2 h-4 w-4 animate-spin" /> | ||
)} | ||
{authType === 'signup' ? ( | ||
<p>Sign Up with Email</p> | ||
) : ( | ||
<p>Sign In with Email</p> | ||
)} | ||
</Button> | ||
</div> | ||
</form> | ||
)} | ||
{otpOpen && ( | ||
<OtpForm email={email} setOtpOpen={setOtpOpen} roleType="user" /> | ||
)} | ||
</div> | ||
); | ||
} |
Oops, something went wrong.