generated from Real-Dev-Squad/website-template
-
Notifications
You must be signed in to change notification settings - Fork 21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add auth store #119
Merged
Merged
add auth store #119
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
9c6e6c0
add auth store
pallabez aafa7fa
Merge branch 'develop' into authentication
pallabez a2b0059
handle api response
pallabez 6e74bec
remove unnecessary comment
pallabez 01d2d0e
Merge branch 'develop' into authentication
pallabez 87de6fd
add apis constant
pallabez bcfbedc
remove test file
pallabez 811478f
handle auth error
pallabez File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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,29 @@ | ||
import { type User } from '~/interfaces/User' | ||
import { type SelfInfo } from './auth.type' | ||
|
||
export const transformSelfInfoFromApi = (userInfo: any): { info: SelfInfo, user: User } => { | ||
const info = { | ||
userId: userInfo?.id, | ||
roles: { | ||
archived: userInfo?.roles?.archived, | ||
member: userInfo?.roles?.member | ||
}, | ||
status: userInfo?.status, | ||
incompleteUserDetails: userInfo?.incompleteUserDetails | ||
} | ||
|
||
const user = { | ||
id: userInfo?.id, | ||
username: userInfo?.username, | ||
firstName: userInfo?.first_name, | ||
lastName: userInfo?.last_name, | ||
githubId: userInfo?.github_id, | ||
githubDisplayName: userInfo?.github_display_name, | ||
avatarUrl: userInfo?.picture?.url | ||
} | ||
|
||
return { | ||
info, | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export interface SelfInfo { | ||
userId: string | ||
roles: { | ||
member: boolean | ||
archived: boolean | ||
} | ||
status: string | ||
incompleteUserDetails: boolean | ||
} |
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,42 @@ | ||
import axios, { isAxiosError } from 'axios' | ||
import { transformSelfInfoFromApi } from './auth.transformer' | ||
import { type ApiResponse } from '~/interfaces/ApiResponse' | ||
import { type User } from '~/interfaces/User' | ||
import { type SelfInfo } from './auth.type' | ||
import { | ||
type ErrorApiForbidden, | ||
type ErrorApiUnauthorized, | ||
type ErrorApiUnavailable, | ||
type ErrorApiNotFound, | ||
type ErrorApiBase | ||
} from '~/interfaces/ErrorApi' | ||
|
||
type GetSelfResponse = ApiResponse< | ||
{ | ||
user: User | ||
info: SelfInfo | ||
}, | ||
ErrorApiForbidden | ErrorApiUnauthorized | ErrorApiUnavailable | ErrorApiNotFound | ErrorApiBase | ||
pallabez marked this conversation as resolved.
Show resolved
Hide resolved
|
||
> | ||
|
||
export const getSelf = async (): Promise<GetSelfResponse> => { | ||
const response: GetSelfResponse = await axios | ||
.get('https://api.realdevsquad.com/users/self/', { withCredentials: true }) | ||
pallabez marked this conversation as resolved.
Show resolved
Hide resolved
|
||
.then(res => ({ data: transformSelfInfoFromApi(res.data) })) | ||
.catch((error) => { | ||
if (isAxiosError(error)) { | ||
if (error.response != null) { | ||
const responseData = error.response.data | ||
return { | ||
error: { | ||
status: responseData.statusCode, | ||
error: responseData.error, | ||
message: responseData.message | ||
} | ||
} | ||
} | ||
} | ||
throw error // or Log here | ||
pallabez marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}) | ||
return response | ||
} |
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,4 @@ | ||
export interface ApiResponse<SuccessResponse, ErrorResponse> { | ||
data?: SuccessResponse | ||
error?: ErrorResponse | ||
} |
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,18 @@ | ||
import { type User } from '~/interfaces/User' | ||
import { type ErrorApiBase } from '~/interfaces/ErrorApi' | ||
|
||
export type AuthState = | ||
| { | ||
kind: 'UNAUTHENTICATED' | ||
isLoading: boolean | ||
error?: ErrorApiBase | ||
} | ||
| { | ||
kind: 'AUTHENTICATED' | ||
isLoading: false | ||
roles: { | ||
archived?: boolean | ||
member?: boolean | ||
} | ||
user: 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
export interface ErrorApiNotFound { | ||
status: 404 | ||
error: 'Not Found' | ||
message: string | ||
} | ||
|
||
export interface ErrorApiUnauthorized { | ||
status: 401 | ||
error: 'Unauthorized' | ||
message: string | ||
} | ||
|
||
export interface ErrorApiForbidden { | ||
status: 403 | ||
error: 'Forbidden' | ||
message: string | ||
} | ||
|
||
export interface ErrorApiUnavailable { | ||
status: 503 | ||
error: 'Service Unavailable' | ||
message: string | ||
} | ||
|
||
export interface ErrorApiBase { | ||
status: number | ||
error: string | ||
message: string | ||
} |
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,93 @@ | ||
import { defineStore } from 'pinia' | ||
import * as authAdapter from '~/adapters/auth' | ||
import { type AuthState } from '~/interfaces/AuthState' | ||
import { type ErrorApiBase } from '~/interfaces/ErrorApi' | ||
import { userRepo } from '~/models/User' | ||
|
||
type AuthStoreState = | ||
| { | ||
kind: 'UNAUTHENTICATED' | ||
isLoading: boolean | ||
error: null | ||
authInfo: null | ||
} | ||
| { | ||
kind: 'ERRORED' | ||
isLoading: false | ||
error: ErrorApiBase | ||
authInfo: null | ||
} | ||
| { | ||
kind: 'AUTHENTICATED' | ||
isLoading: false | ||
error: null | ||
authInfo: { | ||
userId: string | ||
status: string | ||
roles: { | ||
archived?: boolean | ||
member?: boolean | ||
} | ||
} | ||
} | ||
|
||
export const useAuthStore = defineStore({ | ||
id: 'auth-store', | ||
state: (): AuthStoreState => ({ | ||
kind: 'UNAUTHENTICATED', | ||
isLoading: false, | ||
error: null, | ||
authInfo: null | ||
}), | ||
actions: { | ||
async fetchSelf () { | ||
if (this.isLoading) return | ||
|
||
this.isLoading = true | ||
|
||
const { data, error } = await authAdapter.getSelf() | ||
if (error != null) { | ||
this.$patch({ | ||
kind: 'ERRORED', | ||
error, | ||
isLoading: false, | ||
authInfo: null | ||
}) | ||
} else if (data != null) { | ||
this.$patch({ | ||
kind: 'AUTHENTICATED', | ||
authInfo: data.info, | ||
isLoading: false, | ||
error: null | ||
}) | ||
userRepo.save(data.user) | ||
} | ||
} | ||
}, | ||
getters: { | ||
getAuthState (state): AuthState { | ||
if (state.kind === 'UNAUTHENTICATED') { | ||
return { | ||
kind: 'UNAUTHENTICATED', | ||
isLoading: state.isLoading | ||
} | ||
} else if (state.kind === 'ERRORED') { | ||
return { | ||
kind: 'UNAUTHENTICATED', | ||
isLoading: false, | ||
error: state.error | ||
} | ||
} else { | ||
const user = userRepo.find(state.authInfo.userId) | ||
if (user == null) throw new Error('User not found in repository') | ||
|
||
return { | ||
kind: 'AUTHENTICATED', | ||
isLoading: false, | ||
roles: state.authInfo.roles, | ||
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
userInfo: any
???There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We are getting this from API, I don't think we can know the type of userInfo here.
Let me know, if I'm not getting it right
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add types for now once api is integrated you shall be able to fixes if some types are breaking