|
4 | 4 | * See LICENSE file in the project root for full license information |
5 | 5 | */ |
6 | 6 |
|
7 | | -import { startAuthentication } from '@simplewebauthn/browser'; |
8 | 7 | import { useAuth } from '@/AuthProvider'; |
9 | 8 | import PhoneInputWithCountryCode from '@/components/phoneInput'; |
10 | | -import { useInternalAuth } from '@/context/InternalAuthContext'; |
11 | 9 | import React, { useEffect, useState } from 'react'; |
12 | 10 | import { useNavigate } from 'react-router-dom'; |
13 | | - |
14 | 11 | import styles from '@/styles/login.module.css'; |
15 | 12 | import { isPasskeySupported, isValidEmail, isValidPhoneNumber } from '../utils'; |
16 | 13 | import { createFetchWithAuth } from '@/fetchWithAuth'; |
17 | 14 | import AuthFallbackOptions from '@/components/AuthFallbackOptions'; |
18 | 15 |
|
19 | 16 | const Login: React.FC = () => { |
20 | 17 | const navigate = useNavigate(); |
21 | | - const { apiHost, hasSignedInBefore, mode: authMode } = useAuth(); |
22 | | - const { validateToken } = useInternalAuth(); |
| 18 | + const { |
| 19 | + apiHost, |
| 20 | + hasSignedInBefore, |
| 21 | + mode: authMode, |
| 22 | + login, |
| 23 | + handlePasskeyLogin, |
| 24 | + } = useAuth(); |
23 | 25 | const [identifier, setIdentifier] = useState<string>(''); |
24 | 26 | const [email, setEmail] = useState<string>(''); |
25 | 27 | const [mode, setMode] = useState<'login' | 'register'>('register'); |
@@ -76,73 +78,6 @@ const Login: React.FC = () => { |
76 | 78 | return isValidEmail(email) && isValidPhoneNumber(phone); |
77 | 79 | }; |
78 | 80 |
|
79 | | - const handlePasskeyLogin = async () => { |
80 | | - try { |
81 | | - const response = await fetchWithAuth(`/webAuthn/login/start`, { |
82 | | - method: 'POST', |
83 | | - }); |
84 | | - |
85 | | - if (!response.ok) { |
86 | | - console.error('Something went wrong getting webauthn options'); |
87 | | - return; |
88 | | - } |
89 | | - |
90 | | - const options = await response.json(); |
91 | | - const credential = await startAuthentication({ optionsJSON: options }); |
92 | | - |
93 | | - const verificationResponse = await fetchWithAuth(`/webAuthn/login/finish`, { |
94 | | - method: 'POST', |
95 | | - body: JSON.stringify({ assertionResponse: credential }), |
96 | | - }); |
97 | | - |
98 | | - if (!verificationResponse.ok) { |
99 | | - console.error('Failed to verify passkey'); |
100 | | - } |
101 | | - |
102 | | - const verificationResult = await verificationResponse.json(); |
103 | | - |
104 | | - if (verificationResult.message === 'Success') { |
105 | | - if (verificationResult.mfaLogin) { |
106 | | - navigate('/mfaLogin'); |
107 | | - return; |
108 | | - } |
109 | | - await validateToken(); |
110 | | - navigate('/'); |
111 | | - return; |
112 | | - } else { |
113 | | - console.error('Passkey login failed:', verificationResult.message); |
114 | | - } |
115 | | - } catch (error) { |
116 | | - console.error('Passkey login error:', error); |
117 | | - } |
118 | | - }; |
119 | | - |
120 | | - const login = async () => { |
121 | | - setFormErrors(''); |
122 | | - |
123 | | - const response = await fetchWithAuth(`/login`, { |
124 | | - method: 'POST', |
125 | | - body: JSON.stringify({ identifier, passkeyAvailable }), |
126 | | - }); |
127 | | - |
128 | | - if (!response.ok) { |
129 | | - setFormErrors('Failed to send login link. Please try again.'); |
130 | | - return; |
131 | | - } |
132 | | - |
133 | | - if (!passkeyAvailable) { |
134 | | - setShowFallbackOptions(true); |
135 | | - return; |
136 | | - } |
137 | | - |
138 | | - try { |
139 | | - await handlePasskeyLogin(); |
140 | | - } catch (err) { |
141 | | - console.error('Passkey login failed', err); |
142 | | - setShowFallbackOptions(true); |
143 | | - } |
144 | | - }; |
145 | | - |
146 | 81 | const register = async () => { |
147 | 82 | setFormErrors(''); |
148 | 83 |
|
@@ -188,7 +123,7 @@ const Login: React.FC = () => { |
188 | 123 | return; |
189 | 124 | } |
190 | 125 |
|
191 | | - navigate('/magic-link-sent'); |
| 126 | + navigate('/magiclinks-sent'); |
192 | 127 | } catch (err) { |
193 | 128 | console.error(err); |
194 | 129 | setFormErrors('Failed to send magic link.'); |
@@ -217,7 +152,18 @@ const Login: React.FC = () => { |
217 | 152 | const handleSubmit = async (e: React.FormEvent<HTMLFormElement>) => { |
218 | 153 | e.preventDefault(); |
219 | 154 |
|
220 | | - if (mode === 'login') login(); |
| 155 | + if (mode === 'login') { |
| 156 | + const loginRes = await login(identifier, passkeyAvailable); |
| 157 | + |
| 158 | + if (loginRes.ok && passkeyAvailable) { |
| 159 | + const passkeyResult = await handlePasskeyLogin(); |
| 160 | + if (passkeyResult) { |
| 161 | + navigate('/'); |
| 162 | + } |
| 163 | + } else { |
| 164 | + setShowFallbackOptions(true); |
| 165 | + } |
| 166 | + } |
221 | 167 | if (mode === 'register') register(); |
222 | 168 | }; |
223 | 169 |
|
|
0 commit comments