-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAuth.jsx
More file actions
111 lines (100 loc) · 2.85 KB
/
Copy pathAuth.jsx
File metadata and controls
111 lines (100 loc) · 2.85 KB
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
import React, { useEffect, useState, useContext } from 'react'
import { observer } from 'mobx-react-lite'
import { Link, useNavigate, useSearchParams } from 'react-router-dom'
import Button from '../components/Button'
import state from '../contexts/state'
export default observer(() => {
const navigate = useNavigate()
const [params, setParams] = useSearchParams()
const { auth, user } = useContext(state)
const [isLoading, setIsLoading] = useState(false)
const [errorMsg, setErrorMsg] = useState('')
const [idInput, setIdInput] = useState('')
const signup = async (platform, access_token) => {
setErrorMsg('')
setIsLoading(true)
try {
await user.signup(platform, access_token)
await user.getRep(platform)
return navigate('/user')
} catch (e) {
setErrorMsg(e.toString())
setIsLoading(false)
}
}
useEffect(() => {
const platform = params.get('platform')
const access_token = params.get('access_token')
const signupError = params.get('signupError')
const isSigningUp = params.get('isSigningUp')
if (platform && access_token) {
if (isSigningUp && parseInt(isSigningUp)) {
signup(platform, access_token)
} else {
user.storeAccessToken(platform, access_token)
}
} else if (signupError) {
setErrorMsg(
`Sign up through ${platform.toUpperCase()} error: ${signupError}`
)
}
setParams('')
}, [])
const login = async () => {
try {
await user.login(idInput)
return navigate('/user')
} catch (e) {
setErrorMsg(e.toString())
}
}
const onIdInputChange = (event) => {
setIdInput(event.target.value)
}
return (
<>
{isLoading ? (
<div className="join-container">
<Loader active inline="centered" size="huge" />
<Link to="/help">Any question?</Link>
</div>
) : (
<div className="join-container">
{errorMsg.length > 0 && (
<Message error header="Oops!" content={errorMsg} />
)}
<Button
basic
color="blue"
size="huge"
onClick={() => auth.join('twitter')}
>
<span>Join with Twitter</span>
</Button>
<Button
basic
color="black"
size="huge"
onClick={() => auth.join('github')}
>
<span>Join with Github</span>
</Button>
<div>Already has account?</div>
</div>
)}
</>
)
})
{
/* <Form>
<TextArea
placeholder="Please enter your private key."
style={{ width: '300px' }}
onChange={onIdInputChange}
/>
</Form>
<Button basic size="large" onClick={login}>
Log in
</Button>
<Link to="/help">Any question?</Link> */
}