Skip to content

Commit

Permalink
Give access to access token in NextJS React hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
frankie567 committed Apr 2, 2023
1 parent 7360d5c commit 35a5497
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 27 deletions.
15 changes: 0 additions & 15 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,21 +81,6 @@ export interface FiefAccessTokenInfo {
access_token: string;
}

/**
* A safe version of {@link FiefAccessTokenInfo}, without `access_token`.
*
* **Example:**
*
* ```json
* {
* "id": "aeeb8bfa-e8f4-4724-9427-c3d5af66190e",
* "scope": ["openid", "required_scope"],
* "permissions": ["castles:read", "castles:create", "castles:update", "castles:delete"],
* }
* ```
*/
export type FiefSafeAccessTokenInfo = Omit<FiefAccessTokenInfo, 'access_token'>;

/**
* Dictionary containing user information.
*
Expand Down
10 changes: 2 additions & 8 deletions src/nextjs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import { pathToRegexp } from 'path-to-regexp';
import {
Fief,
FiefAccessTokenInfo,
FiefSafeAccessTokenInfo,
FiefUserInfo,
} from '../client';
import {
Expand Down Expand Up @@ -474,18 +473,13 @@ class FiefAuth {
*/
public currentUser(): FiefNextApiHandler<{
userinfo: FiefUserInfo | null,
access_token_info: FiefSafeAccessTokenInfo | null,
access_token_info: FiefAccessTokenInfo | null,
}> {
return async (req, res) => {
const refresh = req.query.refresh === 'true';
return this.authenticated(
async (_req, _res) => {
let safeAccessTokenInfo: FiefSafeAccessTokenInfo | null = null;
if (_req.accessTokenInfo) {
const { access_token: _, ...rest } = _req.accessTokenInfo;
safeAccessTokenInfo = rest;
}
_res.status(200).json({ userinfo: _req.user, access_token_info: safeAccessTokenInfo });
_res.status(200).json({ userinfo: _req.user, access_token_info: _req.accessTokenInfo });
},
{ optional: true, refresh },
)(req, res as NextApiResponse);
Expand Down
8 changes: 4 additions & 4 deletions src/nextjs/react.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import React, {
useReducer,
} from 'react';

import { FiefSafeAccessTokenInfo, FiefUserInfo } from '../client';
import { FiefAccessTokenInfo, FiefUserInfo } from '../client';

export interface FiefAuthState {
userinfo: FiefUserInfo | null;
accessTokenInfo: FiefSafeAccessTokenInfo | null;
accessTokenInfo: FiefAccessTokenInfo | null;
}

interface SetUserInfoAuthReducerAction {
Expand All @@ -25,7 +25,7 @@ interface ClearUserInfoAuthReducerAction {

interface SetAccessTokenInfoAuthReducerAction {
type: 'setAccessTokenInfo';
value: FiefSafeAccessTokenInfo;
value: FiefAccessTokenInfo;
}

interface ClearAccessTokenInfoAuthReducerAction {
Expand Down Expand Up @@ -169,7 +169,7 @@ export const useFiefUserinfo = (): FiefUserInfo | null => {
* const accessTokenInfo = useFiefAccessTokenInfo();
* ```
*/
export const useFiefAccessTokenInfo = (): FiefSafeAccessTokenInfo | null => {
export const useFiefAccessTokenInfo = (): FiefAccessTokenInfo | null => {
const { state } = useContext(FiefAuthContext);
return state.accessTokenInfo;
};
Expand Down

0 comments on commit 35a5497

Please sign in to comment.