/
EmailVerification.jsx
140 lines (124 loc) · 3.79 KB
/
EmailVerification.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import React from 'react';
import { Link } from 'react-router-dom';
import alertifyjs from 'alertifyjs';
import { Paper, Button, CircularProgress } from '@material-ui/core';
import MuiAlert from '@material-ui/lab/Alert';
import { get } from 'lodash';
import APIService from '../../services/APIService';
import { refreshCurrentUserCache } from '../../common/utils';
class EmailVerification extends React.Component {
constructor(props) {
super(props);
this.state = {
validToken: true,
user: null,
isLoading: true,
username: props.match.params.user,
token: props.match.params.token,
failureMsg: null,
notFound: false,
}
}
componentDidMount() {
this.fetchUser()
}
fetchUser() {
const { username } = this.state
APIService.users(username).get(null, null, {includeVerificationToken: true}).then(response => {
if(get(response, 'detail') === 'Not found.')
this.setState({notFound: true, isLoading: false})
else
this.setState({user: response.data, isLoading: false, validToken: this.state.token === response.data.verification_token})
})
}
onSubmit() {
const { username, token } = this.state
APIService.users(username).verify(token).get().then(response => {
if(get(response, 'status') === 200 && get(response, 'data.token')) {
alertifyjs.success('Successfully Verified Email.')
this.afterLoginSuccess(response.data.token)
} else if(get(response, 'detail'))
this.setState({failureMsg: response.detail})
else
alertifyjs.error('Something bad happend')
})
}
afterLoginSuccess(token) {
localStorage.setItem('token', token)
this.cacheUserData()
}
cacheUserData() {
refreshCurrentUserCache(response => {
alertifyjs.success(`Successfully signed in as ${this.state.username}.`)
window.location.hash = '#' + response.data.url
})
}
getNotFoundDOM() {
return (
<MuiAlert elevation={6} variant="filled" severity="error">
Unable to verify the user. Either verification link expired or wrong. Please contact OCL Team.
</MuiAlert>
)
}
getAlreadyVerifiedDOM() {
return (
<MuiAlert elevation={6} variant="filled" severity="default">
This user is already verified. Please proceed to <Link to='/accounts/login'>SignIn</Link>.
</MuiAlert>
)
}
getFailureMsgDOM() {
return (
<MuiAlert elevation={6} variant="filled" severity="error">
{
this.state.failureMsg
}
</MuiAlert>
)
}
getConfirmEmailAddressDOM() {
const { user } = this.state;
return (
<React.Fragment>
<h1 style={{textAlign: 'center'}}>Confirm Email Address</h1>
<p>
{
`Please confirm that ${user.email} is an email address for user ${user.username}.`
}
</p>
<Button onClick={() => this.onSubmit()} variant='outlined' color='primary'>
Confirm
</Button>
</React.Fragment>
)
}
getDOM() {
const { user, notFound, failureMsg, validToken } = this.state;
if(notFound || !validToken)
return this.getNotFoundDOM()
if(user.verified)
return this.getAlreadyVerifiedDOM()
if(failureMsg)
return this.getFailureMsgDOM()
return this.getConfirmEmailAddressDOM();
}
render() {
const { isLoading } = this.state;
return (
<div className='col-md-12' style={{marginTop: '25px'}}>
<div className='col-md-3' />
<div className='col-md-6'>
<Paper style={{padding: '25px'}} className='login-paper'>
{
isLoading ?
<CircularProgress /> :
this.getDOM()
}
</Paper>
</div>
<div className='col-md-3' />
</div>
);
}
}
export default EmailVerification;