-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
132 additions
and
9 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { createContext, useContext } from 'react'; | ||
import { AuthStore } from '../features/auth/auth.store'; | ||
|
||
export class RootStore { | ||
authStore: AuthStore; | ||
|
||
constructor() { | ||
this.authStore = new AuthStore(this); | ||
|
||
this._init(); | ||
} | ||
|
||
private _init() { | ||
this.authStore.init(); | ||
} | ||
} | ||
|
||
export const StoreContext = createContext(new RootStore()); | ||
export const useStore = () => useContext(StoreContext); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { AuthCheckResponse } from '@tspark/common'; | ||
import { axiosInstance } from '../../core/api/axios.instance'; | ||
|
||
const authCheck = (): Promise<AuthCheckResponse> => | ||
axiosInstance.get('/auth/auth-check').then((response) => response.data); | ||
|
||
export const authService = { | ||
authCheck, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { UserDto } from '@tspark/common'; | ||
import { RootStore } from '../../core/root.store'; | ||
import { authService } from './auth.service'; | ||
import { action, makeAutoObservable } from 'mobx'; | ||
|
||
export class AuthStore { | ||
private rootStore: RootStore; | ||
|
||
private _isAuthenticated: boolean = false; | ||
private _user?: UserDto; | ||
|
||
constructor(rootStore: RootStore) { | ||
this.rootStore = rootStore; | ||
makeAutoObservable(this); | ||
} | ||
|
||
get isAuthenticated() { | ||
return this._isAuthenticated; | ||
} | ||
|
||
set isAuthenticated(isAuthenticated: boolean) { | ||
this._isAuthenticated = isAuthenticated; | ||
} | ||
|
||
get user() { | ||
return this._user; | ||
} | ||
|
||
set user(user: UserDto | undefined) { | ||
this._user = user; | ||
} | ||
|
||
init() { | ||
this.authCheck(); | ||
} | ||
|
||
authCheck() { | ||
authService.authCheck().then((authCheckResponse) => { | ||
this.isAuthenticated = authCheckResponse.isAuthenticated; | ||
this.user = authCheckResponse.user; | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,19 @@ | ||
import { observer } from 'mobx-react-lite'; | ||
import { useStore } from '../../core/root.store'; | ||
import { Page } from '../page'; | ||
|
||
export const HomePage: React.FC = () => { | ||
export const HomePage: React.FC = observer(function HomePage() { | ||
const { authStore } = useStore(); | ||
|
||
return ( | ||
<Page title="Home"> | ||
<p>{'Hello :)'}</p> | ||
<p>{`Hello${ | ||
authStore.isAuthenticated ? `, ${authStore.user?.username}` : '' | ||
}! :)`}</p> | ||
|
||
<button type="button" onClick={() => authStore.authCheck()}> | ||
CLICK ME | ||
</button> | ||
</Page> | ||
); | ||
}; | ||
}); |