Skip to content

Commit

Permalink
feat: add photoUrl and displayName to user
Browse files Browse the repository at this point in the history
  • Loading branch information
solufa committed Sep 16, 2024
1 parent a709e23 commit 8321281
Show file tree
Hide file tree
Showing 22 changed files with 1,408 additions and 470 deletions.
6 changes: 6 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,16 @@ ARG NEXT_PUBLIC_COGNITO_POOL_ENDPOINT
ARG NEXT_PUBLIC_COGNITO_USER_POOL_ID
ARG NEXT_PUBLIC_COGNITO_USER_POOL_CLIENT_ID
ARG NEXT_PUBLIC_OAUTH_DOMAIN
ARG COGNITO_ACCESS_KEY
ARG COGNITO_SECRET_KEY
ARG COGNITO_REGION
ARG DATABASE_URL

ENV NEXT_PUBLIC_API_BASE_PATH=$NEXT_PUBLIC_API_BASE_PATH
ENV NEXT_PUBLIC_SERVER_PORT=$PORT
ENV COGNITO_ACCESS_KEY=$COGNITO_ACCESS_KEY
ENV COGNITO_SECRET_KEY=$COGNITO_SECRET_KEY
ENV COGNITO_REGION=$COGNITO_REGION

RUN npm run build

Expand Down
17 changes: 15 additions & 2 deletions client/public/docs/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Record<\"server\"|\"db\"|\"s3\",\"ok\">"
"$ref": "#/components/schemas/Record<\"server\"|\"db\"|\"s3\"|\"cognito\",\"ok\">"
}
}
}
Expand Down Expand Up @@ -70,17 +70,25 @@
"signInName": {
"type": "string"
},
"displayName": {
"type": "string"
},
"email": {
"type": "string"
},
"createdTime": {
"type": "number"
},
"photoUrl": {
"type": "string"
}
},
"required": [
"createdTime",
"displayName",
"email",
"id",
"photoUrl",
"signInName"
]
}
Expand Down Expand Up @@ -742,7 +750,7 @@
},
"components": {
"schemas": {
"Record<\"server\"|\"db\"|\"s3\",\"ok\">": {
"Record<\"server\"|\"db\"|\"s3\"|\"cognito\",\"ok\">": {
"type": "object",
"properties": {
"server": {
Expand All @@ -756,9 +764,14 @@
"s3": {
"type": "string",
"const": "ok"
},
"cognito": {
"type": "string",
"const": "ok"
}
},
"required": [
"cognito",
"db",
"s3",
"server"
Expand Down
2 changes: 1 addition & 1 deletion compose.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
services:
magnito:
image: frourio/magnito:0.16.1
image: frourio/magnito:0.17.0
ports:
- 5050:5050
- 5051:5051
Expand Down
3 changes: 3 additions & 0 deletions server/.env.example
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
COGNITO_ACCESS_KEY=magnito-access-key
COGNITO_SECRET_KEY=magnito-secret-key
COGNITO_REGION=ap-northeast-1
S3_ENDPOINT=http://localhost:9000
S3_BUCKET=app
S3_ACCESS_KEY=minio
Expand Down
5 changes: 5 additions & 0 deletions server/api/health/controller.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { cognito } from 'service/cognito';
import { CustomError } from 'service/customAssert';
import { prismaClient } from 'service/prismaClient';
import { s3 } from 'service/s3Client';
Expand All @@ -20,6 +21,10 @@ export default defineController(() => ({
.health()
.then(() => 'ok' as const)
.catch(throwCustomError('S3')),
cognito: await cognito
.health()
.then(() => 'ok' as const)
.catch(throwCustomError('Cognito')),
},
}),
}));
2 changes: 1 addition & 1 deletion server/api/health/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ import type { DefineMethods } from 'aspida';

export type Methods = DefineMethods<{
get: {
resBody: Record<'server' | 'db' | 's3', 'ok'>;
resBody: Record<'server' | 'db' | 's3' | 'cognito', 'ok'>;
};
}>;
2 changes: 2 additions & 0 deletions server/common/types/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import type { DtoId } from './brandedId';
export type UserDto = {
id: DtoId['user'];
signInName: string;
displayName: string;
email: string;
createdTime: number;
photoUrl: string | undefined;
};
21 changes: 15 additions & 6 deletions server/domain/user/model/userMethod.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
import type { AdminGetUserCommandOutput } from '@aws-sdk/client-cognito-identity-provider';
import assert from 'assert';
import { brandedId } from 'service/brandedId';
import type { JwtUser } from 'service/types';
import type { UserEntity } from './userType';

export const userMethod = {
create: (jwtUser: JwtUser): UserEntity => ({
id: brandedId.user.entity.parse(jwtUser.sub),
email: jwtUser.email,
signInName: jwtUser['cognito:username'],
createdTime: Date.now(),
}),
create: (jwtUser: JwtUser, cognitoUser: AdminGetUserCommandOutput): UserEntity => {
const displayName = cognitoUser.UserAttributes?.find((attr) => attr.Name === 'name')?.Value;
assert(displayName);

return {
id: brandedId.user.entity.parse(jwtUser.sub),
email: jwtUser.email,
signInName: jwtUser['cognito:username'],
displayName,
photoUrl: cognitoUser.UserAttributes?.find((attr) => attr.Name === 'picture')?.Value,
createdTime: Date.now(),
};
},
};
9 changes: 8 additions & 1 deletion server/domain/user/repository/userCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,18 @@ export const userCommand = {
save: async (tx: Prisma.TransactionClient, user: UserEntity): Promise<void> => {
await tx.user.upsert({
where: { id: user.id },
update: { email: user.email, signInName: user.signInName },
update: {
email: user.email,
signInName: user.signInName,
displayName: user.displayName,
photoUrl: user.photoUrl,
},
create: {
id: user.id,
email: user.email,
signInName: user.signInName,
displayName: user.displayName,
photoUrl: user.photoUrl,
createdAt: new Date(user.createdTime),
},
});
Expand Down
3 changes: 3 additions & 0 deletions server/domain/user/repository/userQuery.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import type { Prisma, User } from '@prisma/client';
import { brandedId } from 'service/brandedId';
import { z } from 'zod';
import type { UserEntity } from '../model/userType';

const toUserEntity = (prismaUser: User): UserEntity => ({
id: brandedId.user.entity.parse(prismaUser.id),
email: prismaUser.email,
signInName: prismaUser.signInName,
displayName: z.string().parse(prismaUser.displayName),
photoUrl: prismaUser.photoUrl ?? undefined,
createdTime: prismaUser.createdAt.getTime(),
});

Expand Down
6 changes: 5 additions & 1 deletion server/domain/user/useCase/userUseCase.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { UserDto } from 'common/types/user';
import { cognito } from 'service/cognito';
import { prismaClient, transaction } from 'service/prismaClient';
import type { JwtUser } from 'service/types';
import { userMethod } from '../model/userMethod';
Expand All @@ -9,11 +10,14 @@ import { toUserDto } from '../service/toUserDto';
export const userUseCase = {
findOrCreateUser: (jwtUser: JwtUser): Promise<UserDto> =>
transaction('RepeatableRead', async (tx) => {
const cognitoUser = await cognito.getUser(jwtUser);
console.log(111, cognitoUser);
const user = await userQuery.findById(prismaClient, jwtUser.sub).catch(() => null);

if (user !== null) return toUserDto(user);

const newUser = userMethod.create(jwtUser);
const newUser = userMethod.create(jwtUser, cognitoUser);

await userCommand.save(tx, newUser);

return toUserDto(newUser);
Expand Down
Loading

0 comments on commit 8321281

Please sign in to comment.