Skip to content

Commit

Permalink
Merge pull request #27 from side-peek/feature/#3/Store
Browse files Browse the repository at this point in the history
[feature/#3/Store] 전역 상태 관리 초기 설정
  • Loading branch information
Whoknow77 authored Feb 17, 2024
2 parents e393d67 + 4b33cb7 commit aac479e
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 30 deletions.
19 changes: 19 additions & 0 deletions src/constants/user.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { User } from "api-models"

export const AUTH_USER_INITIAL_DATA: User = {
id: "",
nickname: "",
provider: "",
email: "",
password: "",
profile_image_url: "",
introduction: "",
job: "",
career: "",
github_url: "",
blog_url: "",
created_at: "",
updated_at: "",
deleted_at: "",
is_deleted: false,
}
2 changes: 1 addition & 1 deletion src/pages/TestPage/TestPage.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const TestPage = () => {
return <div>TestPage</div>
return <div>Testpage</div>
}

export default TestPage
41 changes: 41 additions & 0 deletions src/stores/authToken.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const { VITE_AUTH_JWT_TOKEN_STORAGE_KEY } = import.meta.env
class AuthToken {
private token: string
private KEY: string

constructor() {
this.token = ""
this.KEY = VITE_AUTH_JWT_TOKEN_STORAGE_KEY
}

setToken(newToken: string) {
try {
const stringifiedData = JSON.stringify(newToken)
localStorage.setItem(this.KEY, stringifiedData)
this.token = newToken
} catch (e) {
this.token = ""
}
}

getToken() {
try {
const res = localStorage.getItem(this.KEY)
if (!res) {
return this.token
}
this.token = JSON.parse(res)
return this.token
} catch (e) {
return this.token
}
}

removeToken() {
localStorage.removeItem(this.KEY)
}
}

const authToken = new AuthToken()

export default authToken
39 changes: 39 additions & 0 deletions src/stores/useAuthStore.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { User } from "api-models"
import { create } from "zustand"

import { AUTH_USER_INITIAL_DATA } from "@/constants/user"

import authToken from "./authToken"

interface AuthUserStore {
user: User
setLogin: (user: User, token: string) => void
setLogout: () => void
updateUser: (user: User) => void
}

const useAuthStore = create<AuthUserStore>()((set) => ({
user: AUTH_USER_INITIAL_DATA,

setLogin: (user, token) => {
authToken.setToken(token)
set(() => ({
user: user,
}))
},

setLogout: () => {
authToken.removeToken()
set(() => ({
user: AUTH_USER_INITIAL_DATA,
}))
},

updateUser: (user: User) => {
set(() => ({
user: user,
}))
},
}))

export default useAuthStore
58 changes: 29 additions & 29 deletions src/types/domain/apiDomain.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
declare module "api-models" {
export interface User {
id: number
id: string
nickname: string
provider: string
email: string
Expand All @@ -18,24 +18,24 @@ declare module "api-models" {
}

export interface UserSkills {
id: number
user_id: number
skill_id: number
id: string
user_id: string
skill_id: string
}

export interface Authorization {
id: number
id: string
refresh_token: string
}

export interface Files {
id: number
id: string
type: string
url: string
}

export interface Project {
id: number
id: string
name: string
sub_name?: string
overview: string
Expand All @@ -50,63 +50,63 @@ declare module "api-models" {
updated_at: string
deleted_at?: string
is_deleted: boolean
view_count: number
like_count: number
view_count: string
like_count: string
}

export interface ProjectTags {
id: number
project_id: number
tag_id: number
id: string
project_id: string
tag_id: string
}

export interface ProjectSkills {
id: number
project_id: number
skill_id: number
id: string
project_id: string
skill_id: string
}

export interface ProjectFiles {
id: number
project_id: number
file_id: number
id: string
project_id: string
file_id: string
}

export interface ProjectMembers {
id: number
project_id: number
member_id?: number
id: string
project_id: string
member_id?: string
authority: string
role: string
nickname: string
}

export interface Comment {
id: number
user_id: number
project_id: number
id: string
user_id: string
project_id: string
is_anonymous: boolean
content: string
created_at: string
updated_at: string
}

export interface Likes {
id: number
user_id: number
project_id: number
id: string
user_id: string
project_id: string
created_at: string
updated_at: string
}

export interface Skill {
id: number
id: string
name: string
icon_url?: string
}

export interface Tag {
id: number
id: string
name: string
}
}
4 changes: 4 additions & 0 deletions src/vite-env.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
/// <reference types="vite/client" />

interface ImportMetaEnv {
readonly VITE_AUTH_JWT_TOKEN_STORAGE_KEY: string
}

0 comments on commit aac479e

Please sign in to comment.