add electron store
This commit is contained in:
parent
ea3fc85371
commit
cc2babdadc
15 changed files with 391 additions and 135 deletions
|
|
@ -1,34 +1,23 @@
|
|||
import { ref, Ref} from "vue";
|
||||
import { ref } from "vue";
|
||||
import { Profile } from "@/types";
|
||||
|
||||
interface UserState {
|
||||
email: string | null;
|
||||
password?: string;
|
||||
key: string | null;
|
||||
}
|
||||
|
||||
const user:Ref<UserState> = ref({
|
||||
email: null,
|
||||
key: "testing"
|
||||
});
|
||||
const profile = ref();
|
||||
|
||||
const CRIMATA_KEY = "CRIMATA_KEY";
|
||||
export const useProfile = () => {
|
||||
|
||||
const raw = window.localStorage.getItem(CRIMATA_KEY);
|
||||
const setProfile = (payload: Profile) => {
|
||||
profile.value = payload;
|
||||
}
|
||||
|
||||
if (raw) {
|
||||
user.value = JSON.parse(raw);
|
||||
}
|
||||
|
||||
export const useAuth = () => {
|
||||
|
||||
const setUser = (token: UserState) => {
|
||||
window.localStorage.setItem(CRIMATA_KEY, JSON.stringify(token));
|
||||
user.value = token;
|
||||
const clearProfile = () => {
|
||||
profile.value = null;
|
||||
}
|
||||
|
||||
return {
|
||||
setUser,
|
||||
user
|
||||
setProfile,
|
||||
clearProfile,
|
||||
profile
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
52
src/modules/http.ts
Normal file
52
src/modules/http.ts
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
|
||||
import axios, { AxiosRequestConfig } from 'axios';
|
||||
|
||||
const baseURL = 'http://127.0.0.1:3010/api';
|
||||
|
||||
|
||||
interface Request {
|
||||
endpoint: string;
|
||||
query?: Record<string, any>;
|
||||
config?: Record<string, any>;
|
||||
}
|
||||
|
||||
|
||||
const makeQuery = (reqQuery: Record<string, any>) => {
|
||||
|
||||
let result = '';
|
||||
|
||||
result = '?' + Object.entries(reqQuery)
|
||||
.map(([ key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(value)}`)
|
||||
.join('&')
|
||||
|
||||
return result;
|
||||
};
|
||||
|
||||
|
||||
export const useHttp = () => {
|
||||
|
||||
const api = axios.create({
|
||||
baseURL,
|
||||
withCredentials: true,
|
||||
});
|
||||
|
||||
|
||||
const post = async (endpoint: string, payload?: Record<string, any>): Promise<any> => (
|
||||
await api.post(endpoint, payload)
|
||||
)
|
||||
|
||||
|
||||
const get = async (req: Request) => {
|
||||
|
||||
if (req.query) {
|
||||
req.endpoint += makeQuery(req.query);
|
||||
}
|
||||
|
||||
const res = await api.get(req.endpoint, req.config);
|
||||
return res;
|
||||
};
|
||||
|
||||
return {
|
||||
get, post
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue