-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* user can verify their account using email * token field is UUID, cleared when verified * adjust verify test * fix verify find * error catch --------- Co-authored-by: Francis Li <mail@francisli.com>
- Loading branch information
Showing
8 changed files
with
159 additions
and
6 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 |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import React from 'react'; | ||
import { LoadingOverlay } from '@mantine/core'; | ||
import { useQuery } from '@tanstack/react-query'; | ||
import { useNavigate, useParams } from 'react-router-dom'; | ||
import { notifications } from '@mantine/notifications'; | ||
/** | ||
* Email Verification | ||
*/ | ||
function Verify() { | ||
const { emailVerificationToken } = useParams(); | ||
const navigate = useNavigate(); | ||
|
||
const { isFetching } = useQuery({ | ||
queryFn: () => | ||
fetch('/api/v1/users/verify', { | ||
method: 'PATCH', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify({ | ||
emailVerificationToken: emailVerificationToken, | ||
}), | ||
}) | ||
.then((response) => { | ||
if (!response.ok) { | ||
return Promise.reject(response); | ||
} | ||
}) | ||
.then(() => { | ||
notifications.show({ | ||
color: 'green', | ||
title: 'Your email address was successfully verified.', | ||
autoClose: 5000, | ||
}); | ||
navigate('/login'); | ||
}) | ||
.catch(() => { | ||
notifications.show({ | ||
color: 'red', | ||
title: `Invalid Email Verification Token`, | ||
autoClose: 5000, | ||
}); | ||
navigate('/'); | ||
}), | ||
}); | ||
return ( | ||
<div> | ||
<LoadingOverlay | ||
visible={isFetching} | ||
zIndex={1000} | ||
overlayProps={{ radius: 'sm', blur: 2 }} | ||
/> | ||
</div> | ||
); | ||
} | ||
|
||
export default Verify; |
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,53 @@ | ||
import { StatusCodes } from 'http-status-codes'; | ||
import User from '../../../../models/user.js'; | ||
|
||
export default async function (fastify, _opts) { | ||
fastify.patch( | ||
'/verify', | ||
{ | ||
schema: { | ||
body: { | ||
type: 'object', | ||
required: ['emailVerificationToken'], | ||
properties: { | ||
emailVerificationToken: { type: 'string' }, | ||
}, | ||
}, | ||
response: { | ||
[StatusCodes.OK]: { | ||
type: 'null', | ||
}, | ||
[StatusCodes.NOT_FOUND]: { | ||
type: 'null', | ||
}, | ||
}, | ||
}, | ||
}, | ||
async (request, reply) => { | ||
const { emailVerificationToken } = request.body; | ||
let data; | ||
try { | ||
data = await fastify.prisma.user.findUnique({ | ||
where: { emailVerificationToken }, | ||
}); | ||
} catch (error) { | ||
return reply.notFound(); | ||
} | ||
if (!data) { | ||
return reply.notFound(); | ||
} | ||
const user = new User(data); | ||
|
||
if (!user.isEmailVerified) { | ||
data = await fastify.prisma.user.update({ | ||
where: { id: user.id }, | ||
data: { | ||
emailVerifiedAt: new Date(), | ||
emailVerificationToken: null, | ||
}, | ||
}); | ||
} | ||
reply.code(StatusCodes.OK); | ||
}, | ||
); | ||
} |
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