|
5 | 5 | import { ButtonAppearance, Views, MessageType } from '../constants'; |
6 | 6 | import Message from './Message.svelte'; |
7 | 7 | import type { AuthorizerState } from '../types'; |
8 | | - import type { VerifyOtpInput } from '@authorizerdev/authorizer-js'; |
| 8 | + import type { VerifyOtpRequest } from '@authorizerdev/authorizer-js'; |
9 | 9 |
|
10 | 10 | export let setView: Function | undefined = undefined; |
11 | 11 | export let onLogin: Function | undefined = undefined; |
|
45 | 45 |
|
46 | 46 | const onSubmit = async () => { |
47 | 47 | componentState.successMessage = null; |
48 | | - const data: VerifyOtpInput = { |
| 48 | + const data: VerifyOtpRequest = { |
49 | 49 | email, |
50 | 50 | otp: componentState.otp || '' |
51 | 51 | }; |
|
54 | 54 | } |
55 | 55 | try { |
56 | 56 | componentState.loading = true; |
57 | | - const res = await state.authorizerRef.verifyOtp(data); |
| 57 | + const { data: res, errors } = await state.authorizerRef.verifyOtp(data); |
58 | 58 | componentState.loading = false; |
| 59 | + if (errors && errors.length) { |
| 60 | + componentState.error = errors[0]?.message || ''; |
| 61 | + return; |
| 62 | + } |
59 | 63 | if (res) { |
60 | 64 | componentState.error = null; |
61 | 65 | state.setAuthData({ |
62 | 66 | user: res.user || null, |
63 | | - token: { |
64 | | - access_token: res.access_token, |
65 | | - expires_in: res.expires_in, |
66 | | - refresh_token: res.refresh_token, |
67 | | - id_token: res.id_token |
68 | | - }, |
| 67 | + token: res, |
69 | 68 | config: state.config, |
70 | 69 | loading: false |
71 | 70 | }); |
|
88 | 87 | componentState.successMessage = null; |
89 | 88 | try { |
90 | 89 | componentState.sendingOtp = true; |
91 | | - const res = await state.authorizerRef.resendOtp({ |
| 90 | + const { data: res, errors } = await state.authorizerRef.resendOtp({ |
92 | 91 | email |
93 | 92 | }); |
94 | 93 | componentState.sendingOtp = false; |
95 | | - if (res && res?.message) { |
| 94 | + if (errors && errors.length) { |
| 95 | + componentState.error = errors[0]?.message || ''; |
| 96 | + return; |
| 97 | + } |
| 98 | + if (res?.message) { |
96 | 99 | componentState.error = null; |
97 | 100 | componentState.successMessage = res.message; |
98 | 101 | } |
|
0 commit comments