Skip to content

Commit

Permalink
feat: adding authenticationType to Auth state (#105)
Browse files Browse the repository at this point in the history
  • Loading branch information
aversini authored Jul 15, 2024
1 parent a85e00f commit 6a315dc
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 0 deletions.
10 changes: 10 additions & 0 deletions packages/auth-provider/src/common/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ import {
ACTION_TYPE_LOGOUT,
} from "./constants";

type AuthenticationTypes =
| typeof AUTH_TYPES.PASSKEY
| typeof AUTH_TYPES.CODE
| typeof AUTH_TYPES.ID_TOKEN
| typeof AUTH_TYPES.ACCESS_TOKEN
| typeof AUTH_TYPES.ID_AND_ACCESS_TOKEN
| null;

export type ServiceCallProps = {
params: any;
clientId: string;
Expand All @@ -23,6 +31,7 @@ export type AuthState = {
isLoading: boolean;
isAuthenticated: boolean;
logoutReason?: string;
authenticationType: AuthenticationTypes;
user?: {
userId?: string;
username?: string;
Expand Down Expand Up @@ -54,6 +63,7 @@ export type InternalActions =
| {
type: typeof ACTION_TYPE_LOGIN;
payload: {
authenticationType: AuthenticationTypes;
user: {
userId: string;
username: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const stub = (): never => {
export const AuthContext = createContext<AuthContextProps>({
isAuthenticated: false,
isLoading: false,
authenticationType: null,
login: stub,
logout: stub,
getAccessToken: stub,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export const AuthProvider = ({
const [state, dispatch] = useReducer(reducer, {
isLoading: true,
isAuthenticated: false,
authenticationType: null,
user: undefined,
logoutReason: "",
});
Expand Down Expand Up @@ -125,6 +126,7 @@ export const AuthProvider = ({
dispatch({
type: ACTION_TYPE_LOGIN,
payload: {
authenticationType: jwt.payload[JWT.AUTH_TYPE_KEY] as string,
user: {
userId: jwt.payload[JWT.USER_ID_KEY] as string,
username: jwt.payload[JWT.USERNAME_KEY] as string,
Expand Down Expand Up @@ -182,6 +184,7 @@ export const AuthProvider = ({
dispatch({
type: ACTION_TYPE_LOGIN,
payload: {
authenticationType: type,
user: {
userId: response.userId as string,
username,
Expand Down Expand Up @@ -212,6 +215,7 @@ export const AuthProvider = ({
dispatch({
type: ACTION_TYPE_LOGIN,
payload: {
authenticationType: type as string,
user: {
userId: response.userId as string,
username,
Expand Down Expand Up @@ -358,6 +362,7 @@ export const AuthProvider = ({
dispatch({
type: ACTION_TYPE_LOGIN,
payload: {
authenticationType: AUTH_TYPES.PASSKEY,
user: {
userId: response.data.userId as string,
username: response.data.username as string,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export const InternalContext = React.createContext<{
state: {
isLoading: true,
isAuthenticated: false,
authenticationType: null,
user: undefined,
logoutReason: "",
},
Expand Down
2 changes: 2 additions & 0 deletions packages/auth-provider/src/components/AuthProvider/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export const reducer = (state: AuthState, action: InternalActions) => {
isLoading: false,
isAuthenticated: true,
user: action.payload.user,
authenticationType: action.payload.authenticationType,
logoutReason: "",
};
}
Expand All @@ -29,6 +30,7 @@ export const reducer = (state: AuthState, action: InternalActions) => {
isLoading: false,
isAuthenticated: false,
user: undefined,
authenticationType: null,
logoutReason: action.payload.logoutReason,
};
}
Expand Down

0 comments on commit 6a315dc

Please sign in to comment.