Skip to content

Commit

Permalink
Merge branch 'main' into fix/issues
Browse files Browse the repository at this point in the history
  • Loading branch information
nunocaseiro authored Nov 30, 2022
2 parents 1ad3a4a + 9a2d814 commit 772edc2
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type CardBodyProps = {
const CardBody = React.memo<CardBodyProps>(({ userId, team }) => {
const { data: session } = useSession();

const isSAdmin = session?.isSAdmin;
const isSAdmin = session?.user.isSAdmin;

const { _id: id, users } = team;

Expand Down
85 changes: 39 additions & 46 deletions frontend/src/pages/api/auth/[...nextauth].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import CredentialsProvider from 'next-auth/providers/credentials';

import { createOrLoginUserAzure, login, refreshAccessToken } from '@/api/authService';
import { Token } from '@/types/token';
import { LoginUser, User } from '@/types/user/user';
import { LoginUser } from '@/types/user/user';
import {
CLIENT_ID,
CLIENT_SECRET,
Expand All @@ -17,13 +17,12 @@ import {
} from '@/utils/constants';
import { DASHBOARD_ROUTE, ERROR_500_PAGE, START_PAGE_ROUTE } from '@/utils/routes';

async function getNewAccessToken(prevToken: JWT) {
async function getNewAccessToken(prevToken: JWT): Promise<JWT> {
try {
const data: Token = await refreshAccessToken(prevToken.refreshToken);
const data: Token = await refreshAccessToken(prevToken.user.refreshToken.token);
return {
...prevToken,
accessToken: data.token,
accessTokenExpires: Date.now() + +data.expiresIn * 1000,
accessToken: { token: data.token, expiresIn: String(Date.now() + +data.expiresIn * 1000) },
error: '',
};
} catch (error) {
Expand All @@ -49,24 +48,20 @@ export default NextAuth({
email: credentials?.email,
password: credentials?.password,
};
const data: User = await login(loginUser);
const data = await login(loginUser);
const { firstName, lastName, isSAdmin, accessToken, refreshToken, id } = data || {};
if (!id || !accessToken || !refreshToken) return null;

if (data && data.id && data.accessToken && data.refreshToken) {
const token = {
firstName: data.firstName,
lastName: data.lastName,
email: data.email,
id: data.id,
accessToken: data.accessToken.token,
refreshToken: data.refreshToken.token,
isSAdmin: data.isSAdmin,
accessTokenExpiresIn: data.accessToken.expiresIn,
refreshTokenExpiresIn: data.refreshToken.expiresIn,
strategy: 'local',
};
return token;
}
return null;
const token = {
firstName,
lastName,
isSAdmin,
accessToken,
refreshToken,
id,
strategy: 'local',
};
return token;
},
}),
],
Expand All @@ -89,9 +84,8 @@ export default NextAuth({
const { firstName, lastName, accessToken, refreshToken, email, id, isSAdmin } = data;
user.firstName = firstName;
user.lastName = lastName;
user.accessToken = accessToken.token;
user.accessTokenExpiresIn = accessToken.expiresIn;
user.refreshToken = refreshToken.token;
user.accessToken = accessToken;
user.refreshToken = refreshToken;
user.email = email;
user.strategy = 'azure';
user.id = id;
Expand All @@ -102,37 +96,36 @@ export default NextAuth({
},
async jwt({ token, user, account }) {
if (account && user) {
return {
accessToken: user.accessToken,
accessTokenExpires: Date.now() + +user.accessTokenExpiresIn * 1000,
refreshToken: user.refreshToken,
id: user.id,
firstName: user.firstName,
lastName: user.lastName,
email: token.email,
const jwt: JWT = {
user: {
accessToken: {
token: user.accessToken.token,
expiresIn: String(Date.now() + +user.accessToken.expiresIn * 1000),
},
id: user.id,
firstName: user.firstName,
lastName: user.lastName,
email: user.email ?? '',
isSAdmin: user.isSAdmin,
refreshToken: user.refreshToken,
},
strategy: user.strategy ?? 'local',
error: '',
isSAdmin: user.isSAdmin,
};

return jwt;
}
if (Date.now() < token.accessTokenExpires - 5000) {

if (Date.now() < +token.user.accessToken.expiresIn - 5000) {
return token;
}

return getNewAccessToken(token);
},
async session({ session, token }) {
const newSession: Session = { ...session };
let newSession: Session = { ...session };
if (token) {
newSession.user.firstName = token.firstName;
newSession.user.lastName = token.lastName;
newSession.accessToken = token.accessToken;
newSession.refreshToken = token.refreshToken;
newSession.user.email = token.email;
newSession.user.id = token.id;
newSession.error = token.error;
newSession.expires = token.accessTokenExpires;
newSession.strategy = token.strategy;
newSession.isSAdmin = token.isSAdmin;
newSession = { ...token, expires: token.user.accessToken.expiresIn };
}
return newSession;
},
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/pages/boards/[boardId].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ const Board: NextPage<Props> = ({ boardId, mainBoardId }) => {
// Show board settings button if current user is allowed to edit
const isResponsibleInSubBoard = isSubBoard && isResponsible;
const hasAdminRole =
isStakeholderOrAdmin || session?.isSAdmin || isOwner || isResponsibleInSubBoard;
isStakeholderOrAdmin || session?.user.isSAdmin || isOwner || isResponsibleInSubBoard;

// Show Alert message if any sub-board wasn't merged
const showMessageHaveSubBoardsMerged =
Expand Down Expand Up @@ -168,7 +168,7 @@ const Board: NextPage<Props> = ({ boardId, mainBoardId }) => {
isOpen={isOpen}
isOwner={isOwner}
isResponsible={isResponsible}
isSAdmin={session?.isSAdmin}
isSAdmin={session?.user.isSAdmin}
isStakeholderOrAdmin={isStakeholderOrAdmin}
setIsOpen={setIsOpen}
socketId={socketId}
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/pages/boards/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const Boards = () => {
<Flex direction="column">
<Suspense fallback={<LoadingPage />}>
<QueryError>
<MyBoards isSuperAdmin={session.isSAdmin} userId={session.user.id} />
<MyBoards isSuperAdmin={session.user.isSAdmin} userId={session.user.id} />
</QueryError>
</Suspense>
</Flex>
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/pages/boards/new.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ const NewBoard: NextPage = () => {
(teamUser) =>
teamUser.user._id === session?.user.id &&
[TeamUserRoles.ADMIN, TeamUserRoles.STAKEHOLDER].includes(teamUser.role),
) || session?.isSAdmin
) || session?.user.isSAdmin
: false;
if (!isAdminOrStakeHolder && !haveError) {
setHaveError(!isAdminOrStakeHolder);
Expand Down
31 changes: 15 additions & 16 deletions frontend/src/types/next-auth.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
import { DefaultSession } from 'next-auth';
import { Token } from './token';

declare module 'next-auth' {
interface Session {
Expand All @@ -8,20 +9,17 @@ declare module 'next-auth' {
firstName: string;
lastName: string;
email: string;
isSAdmin: boolean;
accessToken: Token;
refreshToken?: Token;
} & DefaultSession['user'];
accessToken: string;
refreshToken?: string;
expires: number;
strategy: string;
isSAdmin: boolean;
error: string;
}

interface User {
accessToken: string;
accessTokenExpiresIn: string;
refreshToken: string;
refreshTokenExpiresIn: string;
accessToken: Token;
refreshToken: Token;
strategy: string;
id: string;
firstName: string;
Expand All @@ -32,15 +30,16 @@ declare module 'next-auth' {

declare module 'next-auth/jwt' {
interface JWT {
accessToken: string;
refreshToken: string;
firstName: string;
lastName: string;
email: string;
id: string;
user: {
accessToken: Token;
refreshToken: Token;
firstName: string;
lastName: string;
email: string;
id: string;
isSAdmin: boolean;
};
error: string;
accessTokenExpires: number;
strategy: string;
isSAdmin: boolean;
}
}
2 changes: 1 addition & 1 deletion frontend/src/utils/fetchData.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const nonNeededToken = ['/auth/login', '/auth/refresh', '/auth/registerAzure'];

export const getToken = async (context?: GetServerSidePropsContext) => {
const session = await getSession(context);
if (session) return `Bearer ${session?.accessToken}`;
if (session) return `Bearer ${session?.user.accessToken.token}`;
return 'Bearer ';
};

Expand Down

0 comments on commit 772edc2

Please sign in to comment.