-
Notifications
You must be signed in to change notification settings - Fork 10
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
Dora Noda
committed
Jun 18, 2023
1 parent
a2317db
commit 7fd14c7
Showing
9 changed files
with
128 additions
and
6 deletions.
There are no files selected for viewing
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,15 @@ | ||
import { AuthenticationError } from "apollo-server-errors"; | ||
import { AuthChecker } from "type-graphql"; | ||
import { IContext } from "./context"; | ||
|
||
export const customAuthChecker: AuthChecker<IContext> = ({ | ||
context, | ||
}: { | ||
context: IContext; | ||
}) => { | ||
const { userId } = context; | ||
if (!userId) { | ||
throw new AuthenticationError("Access denied! Please login to continue!"); | ||
} | ||
return true; // or false if access is denied | ||
}; |
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,13 @@ | ||
/* tslint:disable:no-any */ | ||
import { OnefxAuth } from "onefx-auth"; | ||
import { Model } from "@/model/model"; | ||
import { Gateways } from "@/server/gateway/gateway"; | ||
|
||
export interface IContext { | ||
userId: string; | ||
session: any; | ||
model: Model; | ||
gateways: Gateways; | ||
auth: OnefxAuth; | ||
reqHeaders: Record<string, string>; | ||
} |
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,57 @@ | ||
import { | ||
Args, | ||
ArgsType, | ||
Authorized, | ||
Ctx, | ||
Field, | ||
ObjectType, | ||
Query, | ||
} from "type-graphql"; | ||
import { AuthenticationError } from "apollo-server-errors"; | ||
import { IContext } from "@/api-gateway/context"; | ||
|
||
@ArgsType() | ||
class UserProfileRequest { | ||
@Field((_) => String, { nullable: true }) | ||
userId?: string; | ||
} | ||
|
||
@ObjectType() | ||
class UserProfileResponse { | ||
@Field((_) => String) | ||
id: string; | ||
|
||
@Field((_) => String) | ||
email: string; | ||
|
||
@Field((_) => String) | ||
locale: string; | ||
} | ||
|
||
export class UserResolver { | ||
@Authorized() | ||
@Query((_) => UserProfileResponse, { | ||
description: "get the user", | ||
nullable: true, | ||
}) | ||
public async userProfile( | ||
@Args() | ||
args: UserProfileRequest, | ||
@Ctx() | ||
ctx: IContext | ||
): Promise<UserProfileResponse | null> { | ||
const userId = args.userId ?? ctx.userId; | ||
if (String(userId) !== String(ctx.userId)) { | ||
throw new AuthenticationError("not authorized user"); | ||
} | ||
const user = await ctx.auth.user.getById(userId); | ||
if (!user) { | ||
return null; | ||
} | ||
return { | ||
id: user.id, | ||
email: user.email, | ||
locale: user.locale, | ||
}; | ||
} | ||
} |
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,5 +1,7 @@ | ||
import { MyServer } from "@/server/start-server"; | ||
|
||
export type Model = {}; | ||
|
||
export function setModel(server: MyServer): void { | ||
server.model = server.model || {}; | ||
} |
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