Skip to content

Commit f6065cb

Browse files
committed
feat: create user if not any
1 parent 22d9844 commit f6065cb

File tree

6 files changed

+119
-6
lines changed

6 files changed

+119
-6
lines changed

src/apis/gql/gql.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ const documents = {
9696
types.UserDocument,
9797
'\n query User {\n user {\n username\n name\n avatar\n }\n }\n ':
9898
types.UserDocument,
99+
'\n query NumberUsers {\n numberUsers\n }\n ': types.NumberUsersDocument,
100+
'\n mutation CreateUser($username: String!, $password: String!) {\n createUser(username: $username, password: $password)\n }\n ':
101+
types.CreateUserDocument,
99102
'\n query Token($username: String!, $password: String!) {\n token(username: $username, password: $password)\n }\n ':
100103
types.TokenDocument
101104
}
@@ -366,6 +369,18 @@ export function graphql(
366369
export function graphql(
367370
source: '\n query User {\n user {\n username\n name\n avatar\n }\n }\n '
368371
): (typeof documents)['\n query User {\n user {\n username\n name\n avatar\n }\n }\n ']
372+
/**
373+
* The graphql function is used to parse GraphQL queries into a document that can be used by GraphQL clients.
374+
*/
375+
export function graphql(
376+
source: '\n query NumberUsers {\n numberUsers\n }\n '
377+
): (typeof documents)['\n query NumberUsers {\n numberUsers\n }\n ']
378+
/**
379+
* The graphql function is used to parse GraphQL queries into a document that can be used by GraphQL clients.
380+
*/
381+
export function graphql(
382+
source: '\n mutation CreateUser($username: String!, $password: String!) {\n createUser(username: $username, password: $password)\n }\n '
383+
): (typeof documents)['\n mutation CreateUser($username: String!, $password: String!) {\n createUser(username: $username, password: $password)\n }\n ']
369384
/**
370385
* The graphql function is used to parse GraphQL queries into a document that can be used by GraphQL clients.
371386
*/

src/apis/gql/graphql.ts

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1052,6 +1052,17 @@ export type UserQuery = {
10521052
user: { __typename?: 'User'; username: string; name?: string | null; avatar?: string | null }
10531053
}
10541054

1055+
export type NumberUsersQueryVariables = Exact<{ [key: string]: never }>
1056+
1057+
export type NumberUsersQuery = { __typename?: 'Query'; numberUsers: number }
1058+
1059+
export type CreateUserMutationVariables = Exact<{
1060+
username: Scalars['String']['input']
1061+
password: Scalars['String']['input']
1062+
}>
1063+
1064+
export type CreateUserMutation = { __typename?: 'Mutation'; createUser: string }
1065+
10551066
export type TokenQueryVariables = Exact<{
10561067
username: Scalars['String']['input']
10571068
password: Scalars['String']['input']
@@ -2925,6 +2936,63 @@ export const UserDocument = {
29252936
}
29262937
]
29272938
} as unknown as DocumentNode<UserQuery, UserQueryVariables>
2939+
export const NumberUsersDocument = {
2940+
kind: 'Document',
2941+
definitions: [
2942+
{
2943+
kind: 'OperationDefinition',
2944+
operation: 'query',
2945+
name: { kind: 'Name', value: 'NumberUsers' },
2946+
selectionSet: {
2947+
kind: 'SelectionSet',
2948+
selections: [{ kind: 'Field', name: { kind: 'Name', value: 'numberUsers' } }]
2949+
}
2950+
}
2951+
]
2952+
} as unknown as DocumentNode<NumberUsersQuery, NumberUsersQueryVariables>
2953+
export const CreateUserDocument = {
2954+
kind: 'Document',
2955+
definitions: [
2956+
{
2957+
kind: 'OperationDefinition',
2958+
operation: 'mutation',
2959+
name: { kind: 'Name', value: 'CreateUser' },
2960+
variableDefinitions: [
2961+
{
2962+
kind: 'VariableDefinition',
2963+
variable: { kind: 'Variable', name: { kind: 'Name', value: 'username' } },
2964+
type: { kind: 'NonNullType', type: { kind: 'NamedType', name: { kind: 'Name', value: 'String' } } }
2965+
},
2966+
{
2967+
kind: 'VariableDefinition',
2968+
variable: { kind: 'Variable', name: { kind: 'Name', value: 'password' } },
2969+
type: { kind: 'NonNullType', type: { kind: 'NamedType', name: { kind: 'Name', value: 'String' } } }
2970+
}
2971+
],
2972+
selectionSet: {
2973+
kind: 'SelectionSet',
2974+
selections: [
2975+
{
2976+
kind: 'Field',
2977+
name: { kind: 'Name', value: 'createUser' },
2978+
arguments: [
2979+
{
2980+
kind: 'Argument',
2981+
name: { kind: 'Name', value: 'username' },
2982+
value: { kind: 'Variable', name: { kind: 'Name', value: 'username' } }
2983+
},
2984+
{
2985+
kind: 'Argument',
2986+
name: { kind: 'Name', value: 'password' },
2987+
value: { kind: 'Variable', name: { kind: 'Name', value: 'password' } }
2988+
}
2989+
]
2990+
}
2991+
]
2992+
}
2993+
}
2994+
]
2995+
} as unknown as DocumentNode<CreateUserMutation, CreateUserMutationVariables>
29282996
export const TokenDocument = {
29292997
kind: 'Document',
29302998
definitions: [

src/app/(auth)/login/page.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export default function LoginPage() {
2222
})
2323

2424
return (
25-
<div className="mx-auto flex w-full max-w-screen-xl flex-col items-center gap-4 px-4 pt-4 sm:flex-row sm:pt-32">
25+
<div className="mx-auto flex w-full max-w-screen-xl flex-col items-center gap-4 px-4 pt-4 sm:flex-row sm:pt-64">
2626
<div className="flex w-full flex-col items-center sm:w-2/5">
2727
<div className="overflow-hidden rounded-lg">
2828
<RiveComponent src="/809-1634-rocket-demo.riv" className="h-64 w-64" />

src/app/api/login/route.ts

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,43 @@
1-
import { gql, request } from 'graphql-request'
1+
import { request } from 'graphql-request'
22
import { NextResponse } from 'next/server'
3-
import { TokenQuery } from '~/apis/gql/graphql'
3+
import { graphql } from '~/apis/gql'
44
import { storeJWTAsCookie } from '~/helpers'
55

66
export const POST = async (req: Request) => {
77
const { endpointURL, username, password } = await req.json()
88

9-
const { token } = await request<TokenQuery>(
9+
const { numberUsers } = await request(
1010
endpointURL,
11-
gql`
11+
graphql(`
12+
query NumberUsers {
13+
numberUsers
14+
}
15+
`)
16+
)
17+
18+
if (numberUsers === 0) {
19+
const { createUser } = await request(
20+
endpointURL,
21+
graphql(`
22+
mutation CreateUser($username: String!, $password: String!) {
23+
createUser(username: $username, password: $password)
24+
}
25+
`),
26+
{ username, password }
27+
)
28+
29+
storeJWTAsCookie(endpointURL, createUser)
30+
31+
return new NextResponse()
32+
}
33+
34+
const { token } = await request(
35+
endpointURL,
36+
graphql(`
1237
query Token($username: String!, $password: String!) {
1338
token(username: $username, password: $password)
1439
}
15-
`,
40+
`),
1641
{ username, password }
1742
)
1843

src/components/Header.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,7 @@ export const Header: FC = () => {
309309
<ModalSubmitFormFooter
310310
reset={updateProfileForm.reset}
311311
isResetDisabled={!updateProfileFormDirty}
312+
isSubmitDisabled={!updateProfileFormDirty}
312313
isSubmitting={updateProfileForm.formState.isSubmitting}
313314
/>
314315
</ModalContent>

src/styles/globals.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ form {
1616
@apply contents;
1717
}
1818

19+
input {
20+
@apply bg-clip-text;
21+
}
22+
1923
@media screen(sm) {
2024
::-webkit-scrollbar {
2125
@apply block;

0 commit comments

Comments
 (0)