-
Notifications
You must be signed in to change notification settings - Fork 0
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 #1 from javiruiz27/create-libs
feat: verify token
- Loading branch information
Showing
12 changed files
with
182 additions
and
80 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
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,32 +1,41 @@ | ||
import koaRouter from "koa-router"; | ||
import crypto from "crypto"; | ||
const jwt = require('koa-jwt'); | ||
import koaRouter from 'koa-router'; | ||
import crypto from 'crypto'; | ||
import jwt from 'jsonwebtoken'; | ||
|
||
const router = new koaRouter(); | ||
|
||
router.get("hello", "/", (ctx) => { | ||
ctx.body = "<h1>Hello</h1>"; | ||
router.get('hello', '/', (ctx) => { | ||
ctx.body = '<h1>Hello</h1>'; | ||
}); | ||
|
||
router.post('/otp/generate', async (ctx, next) => { | ||
const otp = crypto.randomBytes(2).toString('hex'); | ||
const email = ctx.request.body; | ||
//set in context state temporally | ||
ctx.state[email] = otp; | ||
ctx.body = { otp }; | ||
console.log("hola", ctx.state[email]); | ||
const otp = crypto.randomBytes(2).toString('hex'); | ||
const email = ctx.request.body; | ||
|
||
ctx.app.user = { email: email.email, otp }; | ||
|
||
ctx.body = { otp }; | ||
|
||
next(); | ||
}); | ||
|
||
router.post('/otp/verify', async (ctx, next) => { | ||
const userOtp = ctx.request.body.otp; | ||
const storedOtp = "927100b8f1ee"; //await getStoredOtp(ctx.state.user.id); // retrieve stored OTP | ||
if (userOtp === storedOtp) { | ||
const token = jwt.sign({ userId: ctx.state.user.id }, 'your-secret-key', { expiresIn: '1h' }); | ||
ctx.body = { token }; | ||
} else { | ||
ctx.status = 401; | ||
ctx.body = { error: 'Invalid OTP' }; | ||
} | ||
const { otp: userOtp, email } = ctx.request.body; | ||
console.log('users', ctx.app.user); | ||
|
||
const storedOtp = ctx.app.user?.otp ?? ''; // retrieve stored OTP | ||
|
||
if (userOtp === storedOtp) { | ||
const token = jwt.sign({ user: email }, 'your-secret-key', { | ||
expiresIn: '1h', | ||
}); | ||
ctx.body = { token }; | ||
} else { | ||
ctx.status = 401; | ||
ctx.body = { error: 'Invalid OTP' }; | ||
} | ||
|
||
next(); | ||
}); | ||
|
||
export default router; |
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 |
---|---|---|
@@ -1,32 +1,26 @@ | ||
import { makeStyles, useTheme } from '@mui/material'; | ||
import React from 'react' | ||
import React, { Dispatch, SetStateAction } from 'react'; | ||
import OtpInput from 'react-otp-input'; | ||
|
||
const useStyles = makeStyles(theme => ({ | ||
grid: { | ||
backgroundColor: "grey", | ||
height: "50vh", | ||
textAlign: "center" | ||
}, | ||
avatar: { | ||
margin: theme.spacing(1), | ||
backgroundColor: theme.palette.secondary.main | ||
}, | ||
submit: { | ||
margin: theme.spacing(3, 0, 2) | ||
}, | ||
paper: { | ||
marginTop: theme.spacing(8), | ||
display: "flex", | ||
flexDirection: "column", | ||
alignItems: "center" | ||
} | ||
})); | ||
|
||
const OtpInput = () => { | ||
|
||
return ( | ||
<div>OtpInput</div> | ||
) | ||
type OtpInputProps = { | ||
userToken: string; | ||
setUserToken: Dispatch<SetStateAction<string>>; | ||
} | ||
|
||
export default OtpInput | ||
export const OtpInputComp: React.FC<OtpInputProps> = ({ | ||
userToken, setUserToken | ||
}) =>{ | ||
|
||
return ( | ||
<OtpInput | ||
value={userToken} | ||
onChange={setUserToken} | ||
numInputs={4} | ||
renderSeparator={<span>-</span>} | ||
renderInput={(props) => <input {...props} />} | ||
inputStyle={{ | ||
height: "3em", | ||
width: "3em" | ||
}} | ||
/> | ||
); | ||
} |
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
42 changes: 39 additions & 3 deletions
42
apps/ui/src/app/components/validate-token-form/validate-token-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
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,16 +1,21 @@ | ||
import { createContext, Dispatch, SetStateAction } from "react"; | ||
|
||
interface IAuthenticationContext { | ||
otpToken: string | null; | ||
setOtpToken: Dispatch<SetStateAction<string | null>>; | ||
|
||
email: string | null; | ||
setEmail: Dispatch<SetStateAction<string | null>>; | ||
isWaitingForValidation: boolean; | ||
setIsWaitingForValidation: Dispatch<SetStateAction<boolean>>; | ||
} | ||
|
||
export const authenticationContextInitialValue: IAuthenticationContext = { | ||
otpToken: null, | ||
setOtpToken: () => { | ||
email: null, | ||
setEmail: () => { | ||
/** do nothing */ | ||
}, | ||
isWaitingForValidation: false, | ||
setIsWaitingForValidation: () => { | ||
/** do nothing */ | ||
} | ||
}; | ||
|
||
export const AuthenticationContext = createContext(authenticationContextInitialValue); |
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,28 @@ | ||
import { useMutation } from '@tanstack/react-query'; | ||
import { postValidateToken } from '../services'; | ||
|
||
const VALIDATE_TOKEN_KEY = 'validate_token'; | ||
|
||
interface IValidateMutation { | ||
email: string; | ||
otp: string; | ||
} | ||
|
||
const useValidateToken = () => { | ||
const { data, mutate, error, isSuccess } = useMutation({ | ||
mutationKey: [VALIDATE_TOKEN_KEY], | ||
mutationFn: async ({email, otp}: IValidateMutation) => { | ||
const response = await postValidateToken(email, otp); | ||
return response.token; | ||
}, | ||
}); | ||
|
||
return { | ||
data, | ||
mutate, | ||
error, | ||
isSuccess | ||
}; | ||
}; | ||
|
||
export default useValidateToken; |
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