Skip to content

Commit

Permalink
fix: separate types from routes files
Browse files Browse the repository at this point in the history
  • Loading branch information
rmonnier9 committed Oct 11, 2024
1 parent e0d8b18 commit db34000
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 50 deletions.
19 changes: 19 additions & 0 deletions app/api/auth/agent-connect/agent-connect-types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Exception } from '#models/exceptions';

export class AgentConnectLogoutFailedException extends Exception {
constructor(args: { cause?: any }) {
super({
...args,
name: 'LogoutFailedException',
});
}
}

export class AgentConnectFailedException extends Exception {
constructor(args: { cause?: any }) {
super({
name: 'AgentConnectionFailedException',
...args,
});
}
}
13 changes: 2 additions & 11 deletions app/api/auth/agent-connect/callback/route.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { agentConnectAuthenticate } from '#clients/authentication/agent-connect/strategy';
import { HttpForbiddenError } from '#clients/exceptions';
import { Exception } from '#models/exceptions';
import { getAgent } from '#models/user/agent';
import { logFatalErrorInSentry } from '#utils/sentry';
import { cleanPathFrom, getPathFrom, setAgentSession } from '#utils/session';
import withSession from '#utils/session/with-session';
import { NextResponse } from 'next/server';
import { AgentConnectFailedException } from '../agent-connect-types';

export const GET = withSession(async function callbackRoute(req) {
try {
Expand All @@ -23,20 +23,11 @@ export const GET = withSession(async function callbackRoute(req) {
return NextResponse.redirect('/');
}
} catch (e: any) {
logFatalErrorInSentry(new AgentConnectionFailedException({ cause: e }));
logFatalErrorInSentry(new AgentConnectFailedException({ cause: e }));
if (e instanceof HttpForbiddenError) {
return NextResponse.redirect('/connexion/echec-authorisation-requise');
} else {
return NextResponse.redirect('/connexion/echec-connexion');
}
}
});

export class AgentConnectionFailedException extends Exception {
constructor(args: { cause?: any }) {
super({
name: 'AgentConnectionFailedException',
...args,
});
}
}
4 changes: 2 additions & 2 deletions app/api/auth/agent-connect/login/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { logFatalErrorInSentry } from '#utils/sentry';
import { setPathFrom } from '#utils/session';
import withSession from '#utils/session/with-session';
import { NextResponse } from 'next/server';
import { AgentConnectionFailedException } from '../callback/route';
import { AgentConnectFailedException } from '../agent-connect-types';

export const GET = withSession(async function loginRoute(req) {
try {
Expand All @@ -16,7 +16,7 @@ export const GET = withSession(async function loginRoute(req) {
const url = await agentConnectAuthorizeUrl(req);
return NextResponse.redirect(url);
} catch (e: any) {
logFatalErrorInSentry(new AgentConnectionFailedException({ cause: e }));
logFatalErrorInSentry(new AgentConnectFailedException({ cause: e }));
return NextResponse.redirect('/connexion/echec-connexion');
}
});
4 changes: 2 additions & 2 deletions app/api/auth/agent-connect/logout-callback/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import logErrorInSentry from '#utils/sentry';
import { cleanAgentSession, cleanPathFrom, getPathFrom } from '#utils/session';
import withSession from '#utils/session/with-session';
import { NextResponse } from 'next/server';
import { LogoutFailedException } from '../logout/route';
import { AgentConnectLogoutFailedException } from '../agent-connect-types';

export const GET = withSession(async function logoutCallbackRoute(req) {
try {
Expand All @@ -16,7 +16,7 @@ export const GET = withSession(async function logoutCallbackRoute(req) {
return NextResponse.redirect('/connexion/au-revoir');
}
} catch (e: any) {
logErrorInSentry(new LogoutFailedException({ cause: e }));
logErrorInSentry(new AgentConnectLogoutFailedException({ cause: e }));
return NextResponse.redirect('/connexion/au-revoir');
}
});
13 changes: 2 additions & 11 deletions app/api/auth/agent-connect/logout/route.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,17 @@
import { agentConnectLogoutUrl } from '#clients/authentication/agent-connect/strategy';
import { Exception } from '#models/exceptions';
import logErrorInSentry from '#utils/sentry';
import { setPathFrom } from '#utils/session';
import withSession from '#utils/session/with-session';
import { NextResponse } from 'next/server';
import { AgentConnectLogoutFailedException } from '../agent-connect-types';

export const GET = withSession(async function logoutRoute(req) {
try {
await setPathFrom(req.session, req.headers.get('referer') || '');
const url = await agentConnectLogoutUrl(req);
return NextResponse.redirect(url);
} catch (e: any) {
logErrorInSentry(new LogoutFailedException({ cause: e }));
logErrorInSentry(new AgentConnectLogoutFailedException({ cause: e }));
return NextResponse.redirect('/connexion/au-revoir');
}
});

export class LogoutFailedException extends Exception {
constructor(args: { cause?: any }) {
super({
...args,
name: 'LogoutFailedException',
});
}
}
11 changes: 1 addition & 10 deletions app/api/auth/france-connect/callback/route.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { franceConnectAuthenticate } from '#clients/authentication/france-connect/strategy';
import { Exception } from '#models/exceptions';
import logErrorInSentry from '#utils/sentry';
import { setHidePersonalDataRequestFCSession } from '#utils/session';
import withSession from '#utils/session/with-session';
import { NextResponse } from 'next/server';
import { FranceConnectFailedException } from '../france-connect-types';

export const GET = withSession(async function callbackRoute(req) {
try {
Expand All @@ -24,12 +24,3 @@ export const GET = withSession(async function callbackRoute(req) {
return NextResponse.redirect('/connexion/echec-connexion');
}
});

export class FranceConnectFailedException extends Exception {
constructor(args: { cause?: any }) {
super({
name: 'FranceConnectFailedException',
...args,
});
}
}
19 changes: 19 additions & 0 deletions app/api/auth/france-connect/france-connect-types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Exception } from '#models/exceptions';

export class FranceConnectFailedException extends Exception {
constructor(args: { cause?: any }) {
super({
name: 'FranceConnectFailedException',
...args,
});
}
}

export class FranceConnectLogoutFailedException extends Exception {
constructor(args: { cause?: any }) {
super({
...args,
name: 'LogoutFailedException',
});
}
}
2 changes: 1 addition & 1 deletion app/api/auth/france-connect/login/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { FranceConnectAuthorizeUrl } from '#clients/authentication/france-connec
import { logFatalErrorInSentry } from '#utils/sentry';
import withSession from '#utils/session/with-session';
import { NextResponse } from 'next/server';
import { FranceConnectFailedException } from '../callback/route';
import { FranceConnectFailedException } from '../france-connect-types';

export const GET = withSession(async function loginRoute(req) {
try {
Expand Down
4 changes: 2 additions & 2 deletions app/api/auth/france-connect/logout-callback/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import logErrorInSentry from '#utils/sentry';
import { getPathFrom } from '#utils/session';
import withSession from '#utils/session/with-session';
import { NextResponse } from 'next/server';
import { LogoutFailedException } from '../logout/route';
import { FranceConnectLogoutFailedException } from '../france-connect-types';

export const GET = withSession(async function logoutCallbackRoute(req) {
try {
Expand All @@ -17,7 +17,7 @@ export const GET = withSession(async function logoutCallbackRoute(req) {
return NextResponse.redirect('/connexion/au-revoir');
}
} catch (e: any) {
logErrorInSentry(new LogoutFailedException({ cause: e }));
logErrorInSentry(new FranceConnectLogoutFailedException({ cause: e }));
return NextResponse.redirect('/connexion/au-revoir');
}
});
13 changes: 2 additions & 11 deletions app/api/auth/france-connect/logout/route.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { franceConnectLogoutUrl } from '#clients/authentication/france-connect/strategy';
import { Exception } from '#models/exceptions';
import logErrorInSentry from '#utils/sentry';
import { setPathFrom } from '#utils/session';
import withSession from '#utils/session/with-session';
import { NextResponse } from 'next/server';
import { FranceConnectLogoutFailedException } from '../france-connect-types';

export const GET = withSession(async function logoutRoute(req) {
try {
Expand All @@ -14,16 +14,7 @@ export const GET = withSession(async function logoutRoute(req) {
const url = await franceConnectLogoutUrl(req);
return NextResponse.redirect(url);
} catch (e: any) {
logErrorInSentry(new LogoutFailedException({ cause: e }));
logErrorInSentry(new FranceConnectLogoutFailedException({ cause: e }));
return NextResponse.redirect('/connexion/au-revoir');
}
});

export class LogoutFailedException extends Exception {
constructor(args: { cause?: any }) {
super({
...args,
name: 'LogoutFailedException',
});
}
}

0 comments on commit db34000

Please sign in to comment.