-
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.
- Loading branch information
1 parent
d13391b
commit 31de4ab
Showing
8 changed files
with
152 additions
and
7 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