Skip to content

Commit

Permalink
feat: adding MVP support for access token (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
aversini authored Jun 25, 2024
1 parent 86fb8a0 commit a5a85fd
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 3 deletions.
5 changes: 5 additions & 0 deletions examples/implicit-flow/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ export const App = ({ timeout }: { timeout: string }) => {

<h2>State</h2>
<pre className="text-xs">{JSON.stringify(useAuth(), null, 2)}</pre>

<h2>Access Token</h2>
<pre className="text-xs">
{JSON.stringify(useAuth().getAccessToken(), null, 2)}
</pre>
</Main>
<Footer row1={<p>Timeout: {timeout}</p>} />
</div>
Expand Down
5 changes: 5 additions & 0 deletions packages/auth-common/src/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ sjqybA9mARAqh9k/eiIopecWSiffNQTwVQVd2I9ZH3BalhEXHlqFgrjz51kFqg81
awIDAQAB
-----END PUBLIC KEY-----`;

export const TOKEN_EXPIRATION = {
ACCESS: "5m",
ID: "90d",
};

export const verifyAndExtractToken = async (
token: string,
audience: string,
Expand Down
1 change: 1 addition & 0 deletions packages/auth-provider/src/common/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ export type AuthState = {
export type AuthContextProps = {
login: (username: string, password: string) => Promise<boolean>;
logout: () => void;
getAccessToken: () => string;
} & AuthState;
3 changes: 2 additions & 1 deletion packages/auth-provider/src/common/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export const authenticateUser = async ({
try {
const response = await serviceCall({
params: {
type: AUTH_TYPES.ID_TOKEN,
type: AUTH_TYPES.ID_AND_ACCESS_TOKEN,
username,
password,
sessionExpiration,
Expand All @@ -74,6 +74,7 @@ export const authenticateUser = async ({
if (jwt && jwt.payload[JWT.USER_ID_KEY] !== "") {
return {
idToken: response.data.idToken,
accessToken: response.data.accessToken,
userId: jwt.payload[JWT.USER_ID_KEY] as string,
status: true,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export const AuthContext = createContext<AuthContextProps>({
isLoading: false,
login: stub,
logout: stub,
getAccessToken: stub,
logoutReason: "",
idTokenClaims: null,
});
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ export const AuthProvider = ({
const [idToken, setIdToken, , removeIdToken] = useLocalStorage({
key: `${LOCAL_STORAGE_PREFIX}::${clientId}::@@user@@`,
});
const [accessToken, setAccessToken, , removeAccessToken] = useLocalStorage({
key: `${LOCAL_STORAGE_PREFIX}::${clientId}::@@access@@`,
});

const [authState, setAuthState] = useState<AuthState>({
isLoading: true,
Expand All @@ -42,8 +45,9 @@ export const AuthProvider = ({
idTokenClaims: null,
});
removeIdToken();
removeAccessToken();
},
[removeIdToken],
[removeIdToken, removeAccessToken],
);

/**
Expand Down Expand Up @@ -89,6 +93,7 @@ export const AuthProvider = ({
});
if (response.status) {
setIdToken(response.idToken);
setAccessToken(response.accessToken);
setAuthState({
isLoading: false,
isAuthenticated: true,
Expand All @@ -104,8 +109,14 @@ export const AuthProvider = ({
cleanupSession(LOGOUT_SESSION);
};

const getAccessToken = () => {
return accessToken;
};

return (
<AuthContext.Provider value={{ ...authState, login, logout }}>
<AuthContext.Provider
value={{ ...authState, login, logout, getAccessToken }}
>
{children}
</AuthContext.Provider>
);
Expand Down

0 comments on commit a5a85fd

Please sign in to comment.