Skip to content

Commit

Permalink
feat: add ws login (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
hobroker authored Jun 3, 2022
1 parent 38b3857 commit 44ba3cb
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 8 deletions.
2 changes: 1 addition & 1 deletion schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ input ListRecommendationsInput {
}

type Mutation {
joinWithGoogle(input: JoinWithGoogleInput!): User!
joinWithGoogle(input: JoinWithGoogleInput!): Void!
logout: Void!
readAllNotifications: Void!
readNotification(input: ReadNotificationInput!): Void!
Expand Down
14 changes: 13 additions & 1 deletion src/features/apollo/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import { onError } from '@apollo/client/link/error';
import { GraphQLWsLink } from '@apollo/client/link/subscriptions';
import { createClient } from 'graphql-ws';
import { getMainDefinition } from '@apollo/client/utilities';
import { setContext } from '@apollo/client/link/context';
import { getCookie } from '../../utils/cookie';
import { API_URL, WS_URL } from './constants';
import errorHandler from './errorHandler';

Expand All @@ -17,6 +19,9 @@ export const linkError = onError(errorHandler);
const wsLink = new GraphQLWsLink(
createClient({
url: `${WS_URL}/subscriptions`,
connectionParams: {
authorization: getCookie('Authentication'),
},
}),
);

Expand All @@ -25,6 +30,13 @@ const httpLink = new HttpLink({
credentials: 'include',
});

const authLink = setContext((_, { headers }) => ({
headers: {
...headers,
authorization: getCookie('Authentication'),
},
}));

const splitLink = split(
({ query }) => {
const definition = getMainDefinition(query);
Expand All @@ -35,7 +47,7 @@ const splitLink = split(
);
},
wsLink,
httpLink,
authLink.concat(httpLink),
);

const client = new ApolloClient({
Expand Down
3 changes: 1 addition & 2 deletions src/features/join/features/google/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import { gql } from '@apollo/client';
export const MUTATION_JOIN_WITH_GOOGLE = gql`
mutation JoinWithGoogle($input: JoinWithGoogleInput!) {
joinWithGoogle(input: $input) {
name
email
__typename
}
}
`;
7 changes: 3 additions & 4 deletions src/generated/graphql.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export type ListRecommendationsInput = {

export type Mutation = {
__typename?: 'Mutation';
joinWithGoogle: User;
joinWithGoogle: Void;
logout: Void;
readAllNotifications: Void;
readNotification: Void;
Expand Down Expand Up @@ -398,7 +398,7 @@ export type JoinWithGoogleMutationVariables = Exact<{

export type JoinWithGoogleMutation = {
__typename?: 'Mutation';
joinWithGoogle: { __typename?: 'User'; name: string; email: string };
joinWithGoogle: { __typename: 'Void' };
};

export type ListNotificationsQueryVariables = Exact<{ [key: string]: never }>;
Expand Down Expand Up @@ -1195,8 +1195,7 @@ export type ListUpNextQueryResult = Apollo.QueryResult<
export const JoinWithGoogleDocument = gql`
mutation JoinWithGoogle($input: JoinWithGoogleInput!) {
joinWithGoogle(input: $input) {
name
email
__typename
}
}
`;
Expand Down
13 changes: 13 additions & 0 deletions src/utils/cookie.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/* eslint-disable no-useless-escape */

function escape(string: string) {
return string.replace(/([.*+?\^$(){}|\[\]\/\\])/g, '\\$1');
}

export function getCookie(name: string) {
const match = document.cookie.match(
RegExp('(?:^|;\\s*)' + escape(name) + '=([^;]*)'),
);

return match ? match[1] : '';
}

0 comments on commit 44ba3cb

Please sign in to comment.