-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Login page with basic validation
- Config Codegen for graphql schema and operations - Create auth logic - Create login wall
- Loading branch information
Marcelo Reis
committed
May 2, 2021
1 parent
e871ef5
commit 90b7636
Showing
15 changed files
with
2,637 additions
and
126 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
**/generated/**/* |
File renamed without changes.
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 @@ | ||
overwrite: true | ||
schema: "./schema.graphql" | ||
documents: "src/**/*.graphql" | ||
generates: | ||
src/generated/graphql.ts: | ||
plugins: | ||
- typescript | ||
- typescript-operations | ||
- typescript-react-apollo | ||
config: | ||
withHooks: true | ||
withMutationFn: 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
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,9 +1,43 @@ | ||
import { makeVar, useReactiveVar } from "@apollo/client"; | ||
import { LoginMutation, useLoginMutation } from "../generated/graphql"; | ||
|
||
export const tokenVar = makeVar<string | null>(null); | ||
const auth = JSON.parse(localStorage.getItem("login")!) as | ||
| LoginMutation["login"] | ||
| null; | ||
|
||
export const authVar = makeVar<LoginMutation["login"] | null>(auth); | ||
|
||
authVar.onNextChange((auth) => { | ||
localStorage.setItem("login", JSON.stringify(auth)); | ||
}); | ||
|
||
export const useAuth = () => { | ||
const token = useReactiveVar(tokenVar); | ||
const [loginMutation, result] = useLoginMutation(); | ||
const auth = useReactiveVar(authVar); | ||
|
||
const login = async ({ | ||
username, | ||
password, | ||
}: { | ||
username: string; | ||
password: string; | ||
}) => { | ||
const result = await loginMutation({ | ||
variables: { input: { username, password } }, | ||
}); | ||
|
||
authVar(result.data?.login); | ||
}; | ||
|
||
const logout = () => { | ||
authVar(null); | ||
}; | ||
|
||
return { token, isLogged: !!token }; | ||
return { | ||
user: auth?.user, | ||
loading: result.loading, | ||
error: result.error, | ||
login, | ||
logout, | ||
}; | ||
}; |
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,11 +1,11 @@ | ||
import React, { ComponentProps } from "react"; | ||
import { MockedProvider } from "@apollo/client/testing"; | ||
import { MockedProvider as Provider } from "@apollo/client/testing"; | ||
import { createCache } from "./cache"; | ||
|
||
const createMockedProvider = (props: ComponentProps<typeof MockedProvider>) => { | ||
const MockedProvider = (props: ComponentProps<typeof Provider>) => { | ||
const cache = createCache(); | ||
|
||
return () => <MockedProvider addTypename={false} cache={cache} {...props} />; | ||
return <Provider cache={cache} {...props} />; | ||
}; | ||
|
||
export default createMockedProvider; | ||
export default MockedProvider; |
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,166 @@ | ||
import { gql } from '@apollo/client'; | ||
import * as Apollo from '@apollo/client'; | ||
export type Maybe<T> = T | null; | ||
export type Exact<T extends { [key: string]: unknown }> = { [K in keyof T]: T[K] }; | ||
export type MakeOptional<T, K extends keyof T> = Omit<T, K> & { [SubKey in K]?: Maybe<T[SubKey]> }; | ||
export type MakeMaybe<T, K extends keyof T> = Omit<T, K> & { [SubKey in K]: Maybe<T[SubKey]> }; | ||
const defaultOptions = {} | ||
/** All built-in and custom scalars, mapped to their actual values */ | ||
export type Scalars = { | ||
ID: string; | ||
String: string; | ||
Boolean: boolean; | ||
Int: number; | ||
Float: number; | ||
}; | ||
|
||
export type AddNoteInput = { | ||
activityId: Scalars['ID']; | ||
content: Scalars['String']; | ||
}; | ||
|
||
export type AuthResponseType = { | ||
__typename?: 'AuthResponseType'; | ||
access_token?: Maybe<Scalars['String']>; | ||
user?: Maybe<UserType>; | ||
}; | ||
|
||
export type Call = { | ||
__typename?: 'Call'; | ||
call_type: Scalars['String']; | ||
created_at: Scalars['String']; | ||
direction: Scalars['String']; | ||
duration: Scalars['Float']; | ||
from: Scalars['String']; | ||
id: Scalars['ID']; | ||
is_archived: Scalars['Boolean']; | ||
notes: Array<Note>; | ||
to: Scalars['String']; | ||
via: Scalars['String']; | ||
}; | ||
|
||
export type LoginInput = { | ||
password: Scalars['String']; | ||
username: Scalars['String']; | ||
}; | ||
|
||
export type Mutation = { | ||
__typename?: 'Mutation'; | ||
addNote: Call; | ||
archiveCall: Call; | ||
login: AuthResponseType; | ||
refreshToken: AuthResponseType; | ||
}; | ||
|
||
|
||
export type MutationAddNoteArgs = { | ||
input: AddNoteInput; | ||
}; | ||
|
||
|
||
export type MutationArchiveCallArgs = { | ||
id: Scalars['ID']; | ||
}; | ||
|
||
|
||
export type MutationLoginArgs = { | ||
input: LoginInput; | ||
}; | ||
|
||
export type Note = { | ||
__typename?: 'Note'; | ||
content: Scalars['String']; | ||
id: Scalars['ID']; | ||
}; | ||
|
||
export type PaginatedCalls = { | ||
__typename?: 'PaginatedCalls'; | ||
hasNextPage: Scalars['Boolean']; | ||
nodes?: Maybe<Array<Call>>; | ||
totalCount: Scalars['Int']; | ||
}; | ||
|
||
export type Query = { | ||
__typename?: 'Query'; | ||
call?: Maybe<Call>; | ||
me: UserType; | ||
paginatedCalls: PaginatedCalls; | ||
}; | ||
|
||
|
||
export type QueryCallArgs = { | ||
id: Scalars['ID']; | ||
}; | ||
|
||
|
||
export type QueryPaginatedCallsArgs = { | ||
limit?: Maybe<Scalars['Float']>; | ||
offset?: Maybe<Scalars['Float']>; | ||
}; | ||
|
||
export type Subscription = { | ||
__typename?: 'Subscription'; | ||
onUpdatedCall?: Maybe<Call>; | ||
}; | ||
|
||
export type UserType = { | ||
__typename?: 'UserType'; | ||
id: Scalars['String']; | ||
username: Scalars['String']; | ||
}; | ||
|
||
export type LoginMutationVariables = Exact<{ | ||
input: LoginInput; | ||
}>; | ||
|
||
|
||
export type LoginMutation = ( | ||
{ __typename?: 'Mutation' } | ||
& { login: ( | ||
{ __typename?: 'AuthResponseType' } | ||
& Pick<AuthResponseType, 'access_token'> | ||
& { user?: Maybe<( | ||
{ __typename?: 'UserType' } | ||
& Pick<UserType, 'id' | 'username'> | ||
)> } | ||
) } | ||
); | ||
|
||
|
||
export const LoginDocument = gql` | ||
mutation Login($input: LoginInput!) { | ||
login(input: $input) { | ||
access_token | ||
user { | ||
id | ||
username | ||
} | ||
} | ||
} | ||
`; | ||
export type LoginMutationFn = Apollo.MutationFunction<LoginMutation, LoginMutationVariables>; | ||
|
||
/** | ||
* __useLoginMutation__ | ||
* | ||
* To run a mutation, you first call `useLoginMutation` within a React component and pass it any options that fit your needs. | ||
* When your component renders, `useLoginMutation` returns a tuple that includes: | ||
* - A mutate function that you can call at any time to execute the mutation | ||
* - An object with fields that represent the current status of the mutation's execution | ||
* | ||
* @param baseOptions options that will be passed into the mutation, supported options are listed on: https://www.apollographql.com/docs/react/api/react-hooks/#options-2; | ||
* | ||
* @example | ||
* const [loginMutation, { data, loading, error }] = useLoginMutation({ | ||
* variables: { | ||
* input: // value for 'input' | ||
* }, | ||
* }); | ||
*/ | ||
export function useLoginMutation(baseOptions?: Apollo.MutationHookOptions<LoginMutation, LoginMutationVariables>) { | ||
const options = {...defaultOptions, ...baseOptions} | ||
return Apollo.useMutation<LoginMutation, LoginMutationVariables>(LoginDocument, options); | ||
} | ||
export type LoginMutationHookResult = ReturnType<typeof useLoginMutation>; | ||
export type LoginMutationResult = Apollo.MutationResult<LoginMutation>; | ||
export type LoginMutationOptions = Apollo.BaseMutationOptions<LoginMutation, LoginMutationVariables>; |
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,9 @@ | ||
mutation Login($input: LoginInput!) { | ||
login(input: $input) { | ||
access_token | ||
user { | ||
id | ||
username | ||
} | ||
} | ||
} |
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.