-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #138 from YukiOnishi1129/feature/graphql-auth
feat(web, bff): graphql authentication
- Loading branch information
Showing
16 changed files
with
221 additions
and
19 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { | ||
Injectable, | ||
CanActivate, | ||
ExecutionContext, | ||
UnauthorizedException, | ||
} from '@nestjs/common'; | ||
import { GqlExecutionContext } from '@nestjs/graphql'; | ||
import { createClient } from '@supabase/supabase-js'; | ||
|
||
@Injectable() | ||
export class SupabaseAuthGuard implements CanActivate { | ||
private supabase = createClient( | ||
process.env.SUPABASE_URL, | ||
process.env.SUPABASE_ANON_KEY, | ||
); | ||
|
||
async canActivate(context: ExecutionContext): Promise<boolean> { | ||
const ctx = GqlExecutionContext.create(context).getContext(); | ||
const req = ctx.req || context.switchToHttp().getRequest(); | ||
|
||
const authHeader = req.headers.authorization; | ||
const token = authHeader?.split(' ')[1]; | ||
|
||
if (!token) { | ||
throw new UnauthorizedException('Authorization token not found'); | ||
} | ||
|
||
const { | ||
data: { user }, | ||
error, | ||
} = await this.supabase.auth.getUser(token); | ||
|
||
if (error || !user) { | ||
throw new UnauthorizedException('Invalid or expired token'); | ||
} | ||
|
||
req.user = user; | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { User } from '@supabase/supabase-js'; | ||
import { Request } from 'express'; | ||
|
||
export interface GraphQLContext { | ||
req: Request & { user?: User }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,13 @@ | ||
import { NestFactory } from '@nestjs/core'; | ||
import * as dotenv from 'dotenv'; | ||
|
||
import { AppModule } from './app/app.module'; | ||
|
||
dotenv.config(); | ||
|
||
async function bootstrap() { | ||
const app = await NestFactory.create(AppModule); | ||
await app.listen(3000); | ||
const port = Number(process.env.PORT) || 3000; | ||
await app.listen(port); | ||
} | ||
bootstrap(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,13 @@ | ||
export default function Home() { | ||
return <div>Dashboard</div>; | ||
import { redirect } from "next/navigation"; | ||
|
||
import { ArticleDashboardTemplate } from "@/features/articles/components/Template"; | ||
import { getUser } from "@/features/auth/actions/user"; | ||
|
||
export default async function Dashboard() { | ||
const user = await getUser(); | ||
if (!user) { | ||
redirect("/login"); | ||
} | ||
|
||
return <ArticleDashboardTemplate />; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.