feat: introduce login page
This commit is contained in:
@@ -2,13 +2,22 @@ import { HttpLink, from } from '@apollo/client';
|
||||
import type { ApolloLink } from '@apollo/client';
|
||||
import { onError } from '@apollo/client/link/error';
|
||||
|
||||
import * as URLS from 'config/urls';
|
||||
import { getItem } from 'helpers/storage';
|
||||
|
||||
type CreateLinkOptions = {
|
||||
uri: string;
|
||||
onError?: (message: string) => void;
|
||||
};
|
||||
|
||||
const createHttpLink = (uri: CreateLinkOptions['uri']): ApolloLink => new HttpLink({ uri });
|
||||
const createHttpLink = (uri: CreateLinkOptions['uri']): ApolloLink => {
|
||||
const headers = {
|
||||
authorization: getItem('token') || '',
|
||||
};
|
||||
return new HttpLink({ uri, headers });
|
||||
}
|
||||
|
||||
const NOT_AUTHORISED = 'Not Authorised!';
|
||||
const createErrorLink = (callback: CreateLinkOptions['onError']): ApolloLink => onError(({ graphQLErrors, networkError }) => {
|
||||
if (graphQLErrors)
|
||||
graphQLErrors.forEach(({ message, locations, path }) => {
|
||||
@@ -17,6 +26,10 @@ const createErrorLink = (callback: CreateLinkOptions['onError']): ApolloLink =>
|
||||
console.log(
|
||||
`[GraphQL error]: Message: ${message}, Location: ${locations}, Path: ${path}`,
|
||||
);
|
||||
|
||||
if (message === NOT_AUTHORISED) {
|
||||
window.location.href = URLS.LOGIN;
|
||||
}
|
||||
});
|
||||
|
||||
if (networkError) {
|
||||
|
13
packages/web/src/graphql/mutations/login.ts
Normal file
13
packages/web/src/graphql/mutations/login.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import { gql } from '@apollo/client';
|
||||
|
||||
export const LOGIN = gql`
|
||||
mutation Login($input: LoginInput) {
|
||||
login(input: $input) {
|
||||
token
|
||||
user {
|
||||
id
|
||||
email
|
||||
}
|
||||
}
|
||||
}
|
||||
`;
|
Reference in New Issue
Block a user