Password reset implemetation
parent
99d2a65e17
commit
dded710211
@ -0,0 +1,40 @@
|
||||
import React, {useState} from 'react'
|
||||
import Link from 'next/link'
|
||||
import Router from 'next/router'
|
||||
import Head from 'next/head'
|
||||
import isEmail from 'validator/lib/isEmail'
|
||||
|
||||
import {FormController, Input, Button} from '~/components/form'
|
||||
import useAccountRedirect from '~/hooks/useAccountRedirect'
|
||||
|
||||
export default function ResetPassword(){
|
||||
useAccountRedirect()
|
||||
|
||||
const [submitted, setSubmitted] = useState(false)
|
||||
|
||||
return (
|
||||
<>
|
||||
<Head>
|
||||
<title>Reset Password | Society of Socks</title>
|
||||
</Head>
|
||||
{submitted
|
||||
? (
|
||||
<FormController>
|
||||
<h1>Reset Password</h1>
|
||||
<p>
|
||||
An email has been sent to the provided email address - check your
|
||||
email for further instructions in resetting your password.
|
||||
</p>
|
||||
</FormController>
|
||||
)
|
||||
: (
|
||||
<FormController url="/api/users/recover" afterSubmit={()=>setSubmitted(true)}>
|
||||
<h1>Reset Password</h1>
|
||||
<Input label="Email" type="text" name="email" validate={value=>isEmail(value)} hint="Enter a valid email address" />
|
||||
<Button type="submit">Reset Password</Button>
|
||||
</FormController>
|
||||
)
|
||||
}
|
||||
</>
|
||||
)
|
||||
}
|
@ -0,0 +1,54 @@
|
||||
import React from 'react'
|
||||
import Link from 'next/link'
|
||||
import Router from 'next/router'
|
||||
import axios from 'axios'
|
||||
import Head from 'next/head'
|
||||
|
||||
import {useSetUser} from '~/hooks/useUser'
|
||||
import {FormController, Input, Button} from '~/components/form'
|
||||
import useAccountRedirect from '~/hooks/useAccountRedirect'
|
||||
|
||||
ResetPassword.getInitialProps = async ({ctx}) => {
|
||||
const {id, key} = ctx.query
|
||||
|
||||
return {
|
||||
link_uuid: id,
|
||||
link_key: key
|
||||
}
|
||||
}
|
||||
|
||||
export default function ResetPassword({link_uuid, link_key}){
|
||||
useAccountRedirect()
|
||||
const setUser = useSetUser()
|
||||
|
||||
async function submitReset({password, password2}) {
|
||||
const {data: user} = await axios.post(`/api/users/recover/password`, {
|
||||
password,
|
||||
password2,
|
||||
link_uuid,
|
||||
link_key,
|
||||
})
|
||||
|
||||
setUser(user)
|
||||
|
||||
if (user.is_admin)
|
||||
Router.push('/admin')
|
||||
else
|
||||
Router.push('/account')
|
||||
|
||||
}
|
||||
|
||||
return (
|
||||
<>
|
||||
<Head>
|
||||
<title>Reset Password | Society of Socks</title>
|
||||
</Head>
|
||||
<FormController afterSubmit={submitReset}>
|
||||
<h1>Reset Password</h1>
|
||||
<Input label="Password" type="password" name="password" validate={value=>(value.length >= 8)} hint="Password must be at least 8 characters long" />
|
||||
<Input label="Repeat password" type="password" name="password2" validate={(value, fields)=>(value === fields.password.value)} hint="Passwords must match" />
|
||||
<Button type="submit">Reset Password</Button>
|
||||
</FormController>
|
||||
</>
|
||||
)
|
||||
}
|
@ -0,0 +1,35 @@
|
||||
const db = require('../db')
|
||||
const sendgrid = require('@sendgrid/mail')
|
||||
sendgrid.setApiKey(process.env.SENDGRID_KEY)
|
||||
|
||||
const email = module.exports = {}
|
||||
|
||||
email.sendAccountConfirmation = async user => {
|
||||
const confirmUrl = await db.user.createLoginLink(user.uuid)
|
||||
|
||||
const msg = {
|
||||
to: user.email,
|
||||
from: {email: 'registration@email.societyofsocks.us', name: 'Society of Socks'},
|
||||
templateId: 'd-33407f1dd1b14b7b84dd779511039c95',
|
||||
dynamic_template_data: {
|
||||
confirmUrl: confirmUrl
|
||||
}
|
||||
};
|
||||
|
||||
await sendgrid.send(msg);
|
||||
}
|
||||
|
||||
email.sendPasswordReset = async user => {
|
||||
const resetURL = await db.user.createPasswordReset(user.uuid)
|
||||
|
||||
const msg = {
|
||||
to: user.email,
|
||||
from: {email: 'accounts@email.societyofsocks.us', name: 'Society of Socks'},
|
||||
templateId: 'd-90d751cfc8cd4047be39c994ff9d0f5c',
|
||||
dynamic_template_data: {
|
||||
resetPasswordURL: resetURL
|
||||
}
|
||||
}
|
||||
|
||||
await sendgrid.send(msg)
|
||||
}
|
Loading…
Reference in New Issue