Skip to content

Commit

Permalink
feat: sharing login token between applications
Browse files Browse the repository at this point in the history
  • Loading branch information
Kauacnok committed Jul 2, 2024
1 parent 9e58319 commit 455f8f5
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 9 deletions.
10 changes: 5 additions & 5 deletions next.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const nextConfig = {
permanent: true
},
{
source: '/user/:path',
source: '/user/:path*',
destination: `${process.env.MANAGEMENT_DOMAIN}/user/:path*`,
permanent: true
},
Expand All @@ -38,17 +38,17 @@ const nextConfig = {
permanent: true
},
{
source: '/register/:path',
destination: `${process.env.MANAGEMENT_DOMAIN}/user/:path*`,
source: '/register/:path*',
destination: `${process.env.MANAGEMENT_DOMAIN}/register/:path*`,
permanent: true
},
{
source: '/monitoring/:path',
source: '/monitoring/:path*',
destination: `${process.env.MONITORING_DOMAIN}/monitoring/:path*`,
permanent: true
},
{
source: '/workout/:path',
source: '/workout/:path*',
destination: `${process.env.WORKOUT_DOMAIN}/workout/:path*`,
permanent: true
}
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
"version": "0.1.0",
"private": true,
"scripts": {
"dev": "next dev -p 3001",
"dev": "next dev -p 1213",
"build": "next build",
"start": "next start -p 3001",
"start": "next start -p 1213",
"lint": "next lint"
},
"dependencies": {
Expand Down
33 changes: 33 additions & 0 deletions src/api/auth/RemoveUserCookies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,38 @@ import { cookies } from "next/headers"
export default async function RemoveUserCookies() {
cookies().delete("user")

await fetch(`${process.env.MANAGEMENT_DOMAIN}/api/auth/logout`, {
method: "GET",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
}
}).catch((err) => {
console.log("Erro ao deslogar na aplicação de coaching")
return
})

await fetch(`${process.env.MONITORING_DOMAIN}/api/auth/logout`, {
method: "GET",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
}
}).catch((err) => {
console.log("Erro ao deslogar na aplicação de monitoria")
return
})

await fetch(`${process.env.WORKOUT_DOMAIN}/api/auth/logout`, {
method: "GET",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
}
}).catch((err) => {
console.log("Erro ao deslogar na aplicação de workout")
return
})

return
}
42 changes: 42 additions & 0 deletions src/api/auth/SetUserCookies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,48 @@ export default async function SetUserCookies(response: IResult, status: boolean)
maxAge: 60 * 60 * 24 * 7, // one week
})

const object = {
idUser: response.idUser,
accessToken: response.accessToken,
refreshToken: response.refreshToken
}

await fetch(`${process.env.MANAGEMENT_DOMAIN}/api/auth/login`, {
method: "POST",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
},
body: JSON.stringify(object)
}).catch((err) => {
console.log("Erro ao logar na aplicação de coaching")
return
})

await fetch(`${process.env.MONITORING_DOMAIN}/api/auth/login`, {
method: "POST",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
},
body: JSON.stringify(object)
}).catch((err) => {
console.log("Erro ao logar na aplicação de monitoria")
return
})

await fetch(`${process.env.WORKOUT_DOMAIN}/api/auth/login`, {
method: "POST",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
},
body: JSON.stringify(object)
}).catch((err) => {
console.log("Erro ao logar na aplicação de workout")
return
})

}

}
16 changes: 16 additions & 0 deletions src/app/api/auth/login/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { cookies } from "next/headers";
import { NextRequest, NextResponse } from "next/server";

export async function POST(request: NextRequest) {

const data = await request.json()

cookies().set("user", JSON.stringify(data), {
httpOnly: true,
sameSite: "strict",
secure: false,
maxAge: 60 * 60 * 24 * 7, // one week
})

return NextResponse.json({ message: "Set Cookies" }, { status: 200 });
}
8 changes: 8 additions & 0 deletions src/app/api/auth/logout/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { cookies } from "next/headers";
import { NextResponse } from "next/server";

export async function GET() {
cookies().delete("user")

return NextResponse.json({ message: "Removed Cookies" }, { status: 200 });
}
6 changes: 4 additions & 2 deletions src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,16 @@ export function middleware(request: NextRequest) {
})

if (!validRoute) {
const pathNameWithRegex = pathname.match(/\/[\w-]+\/[\w-]+/g)
const pathNameWithRegex2 = pathNameWithRegex != null ? pathNameWithRegex.join('') : ""
const validActionRoutes: Array<ActionRoutes> = actionRoutes.filter((e) => {
return e.permissions.some((e: number) => {
return e == Number(tokenUserValues.permission)
})
})

validActionRoutes.map((item: ActionRoutes) => {
if (item.route == pathname) {
if (item.route == pathNameWithRegex2) {
validRoute = true
}
})
Expand All @@ -54,5 +56,5 @@ export function middleware(request: NextRequest) {
}

export const config = {
matcher: ['/', '/user', '/user/:path*']
matcher: ['/', '/coaching', '/coaching/:path*']
}

0 comments on commit 455f8f5

Please sign in to comment.