Skip to content

Commit

Permalink
update profile on token refresh
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeRomaa committed Mar 26, 2024
1 parent d731255 commit bca6ac9
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 18 deletions.
4 changes: 4 additions & 0 deletions next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ const nextConfig = {
output: 'standalone',
images: {
remotePatterns: [
{
protocol: 'http',
hostname: 'localhost',
},
{
protocol: 'https',
hostname: 'api.houston.center',
Expand Down
4 changes: 2 additions & 2 deletions src/app/admin/purge/PurgeTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,13 @@ export const PurgeView: React.FC<PurgeViewProps> = ({ data }) => {
},
{
name: 'Tier 1',
selector: (user) => user.t1_active ?? '',
selector: (user) => user.t1_hours ?? '',
sortable: true,
sortFunction: (a, b) => durationToSeconds(a.t1_hours) - durationToSeconds(b.t1_hours),
cell: (user) => (
<div className={classNames('flex items-center', { 'text-green-500': user.t1_active })}>
{user.t1_active && <LuCheck size={20} />}
<p className="w-14 text-right">{user.t1_active}</p>
<p className="w-14 text-right">{user.t1_hours}</p>
</div>
),
width: '125px',
Expand Down
14 changes: 7 additions & 7 deletions src/app/roster/RosterTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,16 +93,16 @@ export const RosterView: React.FC<RosterViewProps> = ({ data }) => {
sortable: true,
},
{
name: 'Unrestricted',
name: 'Unrstd.',
sortable: true,
sortFunction: sortUnrestricted,
cell: (user) => {
if (user.endorsements.app) return <EndorsementBadge tier={0} name="APP" status={user.endorsements.app} />;
if (user.endorsements.twr) return <EndorsementBadge tier={0} name="TWR" status={user.endorsements.twr} />;
return <EndorsementBadge tier={0} name="GND" status={user.endorsements.gnd} />;
},
width: '130px',
right: true,
width: '110px',
center: true,
},
{
name: 'HOU T1',
Expand All @@ -114,7 +114,7 @@ export const RosterView: React.FC<RosterViewProps> = ({ data }) => {
return <EndorsementBadge tier={1} name="HOU" status={false} />;
},
width: '110px',
right: true,
center: true,
},
{
name: 'IAH T1',
Expand All @@ -126,7 +126,7 @@ export const RosterView: React.FC<RosterViewProps> = ({ data }) => {
return <EndorsementBadge tier={1} name="IAH" status={false} />;
},
width: '110px',
right: true,
center: true,
},
{
name: 'I90 T1',
Expand All @@ -137,15 +137,15 @@ export const RosterView: React.FC<RosterViewProps> = ({ data }) => {
return <EndorsementBadge tier={1} name="I90" status={false} />;
},
width: '110px',
right: true,
center: true,
},
{
name: 'ZHU',
sortable: true,
sortFunction: sortZHU,
cell: (user) => <EndorsementBadge tier={2} name="ZHU" status={user.endorsements.zhu} />,
width: '110px',
right: true,
center: true,
},
]}
/>
Expand Down
2 changes: 1 addition & 1 deletion src/app/visit/VisitForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export const VisitForm: React.FC = () => {
className="col-span-3"
disabled
label="Home Facility"
value={session.user.facility}
value={session.user.home_facility}
/>
<TextInput
className="col-span-3"
Expand Down
2 changes: 1 addition & 1 deletion src/types/next-auth.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ type UserId = {
cid: number;
email: string;
rating: string;
facility: string;
home_facility: string;
permissions: {
is_member: boolean;
is_training_staff: boolean;
Expand Down
20 changes: 13 additions & 7 deletions src/utils/auth.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { type AuthOptions } from 'next-auth';
import { type UserId } from '@/types/next-auth';
import type { AuthOptions } from 'next-auth';
import type { UserId } from '@/types/next-auth';

type RefreshedTokens = {
access: string;
refresh: string;
profile: UserId;
}

type JWTPayload = {
Expand Down Expand Up @@ -58,15 +59,20 @@ export const authOptions: AuthOptions = {
if (Date.now() / 1000 > token.account.access_token_exp) {
const body = JSON.stringify({ refresh: token.account.refresh_token });

const refreshResp = await fetch(
// Obtain new token pair and new profile data
const { access, refresh, profile } = await fetch(
`${process.env.NEXT_PUBLIC_API_URL}/auth/token/refresh/`,
{ method: 'POST', headers: { 'Content-Type': 'application/json' }, body },
);

const { access, refresh } = await refreshResp.json() as RefreshedTokens;
{
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body,
},
)
.then<RefreshedTokens>((resp) => resp.json());

return {
...token,
user: profile,
account: {
...token.account,
access_token: access,
Expand Down

0 comments on commit bca6ac9

Please sign in to comment.