|
1 |
| -import { createEffect, createStore } from 'effector' |
2 |
| -import { persist } from 'effector-storage/session' |
3 |
| -import { AuthRequest } from '../model/AuthRequest.ts' |
4 |
| -import { AuthResponse } from '../model/AuthResponse.ts' |
| 1 | +import {createEffect, createStore} from 'effector' |
| 2 | +import {persist} from 'effector-storage/session' |
| 3 | +import {AuthRequest} from '../model/AuthRequest.ts' |
| 4 | +import {AuthResponse} from '../model/AuthResponse.ts' |
5 | 5 | import axios from 'axios'
|
6 |
| -import { AuthState, initialAuthState } from '@/entities' |
7 |
| -import { backendAuthikUrl } from '@/shared/api' |
| 6 | +import {AuthState, initialAuthState} from '@/entities' |
| 7 | +import {backendAuthikUrl} from '@/shared/api' |
8 | 8 |
|
9 | 9 | export const $authStore = createStore<AuthState>(initialAuthState)
|
| 10 | +export const $userIdStore = createStore<number | null>(null); |
10 | 11 |
|
11 |
| -persist({ store: $authStore, key: 'authStore' }) |
| 12 | +export const $userRegisteredStore = createStore<boolean>(false); |
| 13 | + |
| 14 | + |
| 15 | +persist({store: $authStore, key: 'authStore'}) |
| 16 | +persist({store: $userIdStore, key: 'userIdStore'}) |
| 17 | +persist({store: $userRegisteredStore, key: 'userRegisteredStore'}) |
12 | 18 |
|
13 | 19 | export const authFx = createEffect<AuthRequest, AuthState, Error>({
|
14 |
| - handler: async (authReq) => { |
15 |
| - try { |
16 |
| - const response = await axios.put<AuthResponse>( |
17 |
| - `${backendAuthikUrl}/api/auth/telegram/web-app`, |
18 |
| - JSON.stringify(authReq), |
19 |
| - { |
20 |
| - method: 'PUT', |
21 |
| - headers: { |
22 |
| - 'Content-Type': 'application/json', |
23 |
| - }, |
24 |
| - }, |
25 |
| - ) |
26 |
| - return fillAuthState(response.data) |
27 |
| - } catch (error) { |
28 |
| - console.error(error) |
29 |
| - throw new Error('Auth failed') |
30 |
| - } |
31 |
| - }, |
| 20 | + handler: async (authReq) => { |
| 21 | + try { |
| 22 | + const response = await axios.put<AuthResponse>( |
| 23 | + `${backendAuthikUrl}/api/auth/telegram/web-app`, |
| 24 | + JSON.stringify(authReq), |
| 25 | + { |
| 26 | + method: 'PUT', |
| 27 | + headers: { |
| 28 | + 'Content-Type': 'application/json', |
| 29 | + }, |
| 30 | + }, |
| 31 | + ) |
| 32 | + return fillAuthState(response.data) |
| 33 | + } catch (error) { |
| 34 | + console.error(error) |
| 35 | + throw new Error('Auth failed') |
| 36 | + } |
| 37 | + }, |
32 | 38 | })
|
33 | 39 |
|
34 | 40 | const fillAuthState = (response: AuthResponse): AuthState => {
|
35 |
| - return { |
36 |
| - token: response.access, |
37 |
| - loading: false, |
38 |
| - tgId: '-1', |
39 |
| - isAuthenticated: true, |
40 |
| - error: null, |
41 |
| - } |
| 41 | + return { |
| 42 | + token: response.access, |
| 43 | + loading: false, |
| 44 | + tgId: '-1', |
| 45 | + isAuthenticated: true, |
| 46 | + error: null |
| 47 | + }; |
42 | 48 | }
|
43 | 49 |
|
44 |
| -export const $isAuthenticated = $authStore.map((state) => state.isAuthenticated) |
| 50 | +export const $isAuthenticated = $authStore.map(state => state.isAuthenticated) |
| 51 | + |
| 52 | +$authStore.on(authFx.doneData, (_, result) => result); |
| 53 | +$authStore.on(authFx.failData, (_, error) => ({...initialAuthState, loading: false, error: error.message})); |
| 54 | + |
| 55 | +export const userIdFx = createEffect<number | null, number | null, Error>({ |
| 56 | + handler: async (x) => { |
| 57 | + if (x == null) |
| 58 | + throw Error("x is null") |
| 59 | + return x |
| 60 | + } |
| 61 | + } |
| 62 | +) |
| 63 | + |
| 64 | +export const userRegisteredFx = createEffect<boolean, boolean, Error>({ |
| 65 | + handler: async (x) => { |
| 66 | + if (!x) |
| 67 | + throw Error("user not registered") |
| 68 | + return x |
| 69 | + } |
| 70 | + } |
| 71 | +) |
45 | 72 |
|
46 |
| -$authStore.on(authFx.doneData, (_, result) => result) |
47 |
| -$authStore.on(authFx.failData, (_, error) => ({ |
48 |
| - ...initialAuthState, |
49 |
| - loading: false, |
50 |
| - error: error.message, |
51 |
| -})) |
| 73 | +$userIdStore.on(userIdFx.doneData, (_, result) => result) |
| 74 | +$userIdStore.on(userIdFx.failData, (_, __) => { |
| 75 | + return null |
| 76 | +}) |
| 77 | +$userRegisteredStore.on(userRegisteredFx.doneData, (_, result) => result) |
| 78 | +$userRegisteredStore.on(userRegisteredFx.failData, (_, __) => { |
| 79 | + return false |
| 80 | +}) |
0 commit comments