/
sam_single_1
/
Fronend
Обзор
Документация
Войти
/
sam_single_1
/
Fronend
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
src/AuthModule/store/Store.js
109 строк
3 KB
sam_single_1
First Commit
23 окт 2024, 12:13
23 окт 2024, 12:13
7302d97
Код
Авторство
О чём код?
import { makeAutoObservable } from "mobx"; export const states = { NO: "NO", FIRST_LOGIN: "FIRST_LOGIN", VK_LOGIN: "VK_LOGIN", YES: "YES", }; export const storage_items = { state: "state", code_challenge: "code_challenge", code: "code", device_id: "device_id", access_token: "access_token", vk_user_id: "vk_user_id", auth_state: "auth_state", }; class AuthStore { app_auth_state = states.NO; constructor() { makeAutoObservable(this); } initial_check = () => { const state = localStorage.getItem(storage_items.state); const code_challenge = localStorage.getItem(storage_items.code_challenge); const code = localStorage.getItem(storage_items.code); const device_id = localStorage.getItem(storage_items.device_id); const access_token = localStorage.getItem(storage_items.access_token); const vk_user_id = localStorage.getItem(storage_items.vk_user_id); const app_state = localStorage.getItem(storage_items.auth_state); if ( app_state === states.YES && code_challenge != null && state != null && code != null && device_id != null && access_token != null && vk_user_id != null ) { this.app_auth_state = states.YES; } if ( app_state === states.VK_LOGIN && code_challenge != null && state != null && code != null && device_id != null ) { this.app_auth_state = states.VK_LOGIN; } if ( app_state === states.FIRST_LOGIN && code_challenge != null && state != null ) { this.app_auth_state = states.FIRST_LOGIN; } if ( app_state !== states.YES || app_state !== states.VK_LOGIN || app_state !== states.FIRST_LOGIN ) { this.app_auth_state = states.NO; } }; logout = () => { this.app_auth_state = states.NO; localStorage.removeItem(storage_items.state); localStorage.removeItem(storage_items.code_challenge); localStorage.removeItem(storage_items.code); localStorage.removeItem(storage_items.device_id); localStorage.removeItem(storage_items.access_token); localStorage.removeItem(storage_items.vk_user_id); localStorage.removeItem(storage_items.auth_state); }; first_login = (my_state, my_code_challenge) => { this.app_auth_state = states.FIRST_LOGIN; localStorage.setItem(storage_items.state, my_state); localStorage.setItem(storage_items.code_challenge, my_code_challenge); }; vk_login = (my_code, my_device_id) => { this.app_auth_state = states.VK_LOGIN; localStorage.setItem(storage_items.code, my_code); localStorage.setItem(storage_items.device_id, my_device_id); }; second_login = (my_access_token, my_vk_user_id) => { this.app_auth_state = states.YES; localStorage.setItem(storage_items.access_token, my_access_token); localStorage.setItem(storage_items.vk_user_id, my_vk_user_id); }; update_access_token = (my_new_access_token) => { localStorage.setItem(storage_items.access_token, my_new_access_token); }; } export const auth_store = new AuthStore();