Skip to content

Commit

Permalink
fix: Ohjaa käyttäjä kirjautumaan jos käyttäjän kirjautumistiedot ovat…
Browse files Browse the repository at this point in the history
… vanhentuneet (#261)

* fix: Ohjaa käyttäjä kirjautumaan jos käyttäjän kirjautumistiedot ovat vanhentuneet

* WIP

Co-authored-by: Mikko Haapamäki <mikko.haapamaki@cgi.com>
  • Loading branch information
tkork and haapamakim authored Jun 10, 2022
1 parent dd1575a commit 0bd3d95
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 20 deletions.
9 changes: 9 additions & 0 deletions cypress/integration/1 - login/1-login.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
/// <reference types="cypress" />

describe("Login", () => {
it("Ohjaa kirjoutumaton kirjautumissivulle", () => {
cy.intercept("/yllapito/graphql", (req) => req.reply({ statusCode: 302 })).as("graphql");
cy.intercept("/yllapito/kirjaudu", (req) => req.reply({ statusCode: 400, body: "login page" })).as("kirjaudu");
cy.visit(Cypress.env("host") + "/yllapito");
cy.wait("@graphql");
cy.wait("@kirjaudu");
cy.contains("login page");
});

it("Kirjaudu A1 käyttäjänä", () => {
cy.login("A1");
cy.visit(Cypress.env("host") + "/yllapito");
Expand Down
13 changes: 13 additions & 0 deletions next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ function setupLocalDevelopmentMode(config, env) {
env.REACT_APP_API_URL = "http://localhost:3000/graphql";
env.APPSYNC_URL = process.env.REACT_APP_API_URL;

/**
* @type {import("next").NextConfig}
*/
config = {
...config,
env,
Expand All @@ -45,6 +48,16 @@ function setupLocalDevelopmentMode(config, env) {
},
];
},
redirects: () => {
return [
// Just some dummy url to prevent login page loops on local
{
source: "/yllapito/kirjaudu",
destination: "/fake_login_page",
permanent: false,
},
];
},
webpack: (config, { buildId, dev, isServer, defaultLoaders, webpack }) => {
// Important: return the modified config
config.plugins.push(
Expand Down
69 changes: 49 additions & 20 deletions src/services/api/commonApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { ApolloQueryResult } from "apollo-client/core/types";
import { ApolloLink, FetchResult, GraphQLRequest } from "apollo-link";
import { setContext } from "apollo-link-context";
import gql from "graphql-tag";
import ApolloClient, { DefaultOptions } from "apollo-client";
import ApolloClient, { ApolloError, DefaultOptions } from "apollo-client";
import { InMemoryCache, IntrospectionFragmentMatcher } from "apollo-cache-inmemory";
import log from "loglevel";
import { onError } from "apollo-link-error";
Expand All @@ -25,6 +25,23 @@ const defaultOptions: DefaultOptions = {
},
};

// Apollo client is missing type for this
type NetworkError = {
name?: string;
response?: unknown;
statusCode?: number;
bodyText?: string;
};

export class AuthenticationRequiredError extends Error {
constructor(m?: string) {
super(m);

// Set the prototype explicitly.
Object.setPrototypeOf(this, AuthenticationRequiredError.prototype);
}
}

export class API extends AbstractApi {
private publicClient: ApolloClient<any>;
private authenticatedClient: ApolloClient<any>;
Expand Down Expand Up @@ -73,26 +90,38 @@ export class API extends AbstractApi {
}

async callGraphQL(client: ApolloClient<any>, operation: OperationConfig, variables: any) {
switch (operation.operationType) {
case OperationType.Query:
const queryResponse: ApolloQueryResult<any> = await client.query({
query: gql(operation.graphql),
variables,
fetchPolicy: "network-only",
errorPolicy: "all",
fetchResults: true,
});
if (queryResponse.errors && !queryResponse.data) {
log.warn(queryResponse.errors);
throw new Error("API palautti virheen.");
try {
switch (operation.operationType) {
case OperationType.Query:
const queryResponse: ApolloQueryResult<any> = await client.query({
query: gql(operation.graphql),
variables,
fetchPolicy: "network-only",
errorPolicy: "all",
fetchResults: true,
});
if (queryResponse.errors && !queryResponse.data) {
log.warn(queryResponse.errors);
throw new Error("API palautti virheen.");
}
return queryResponse.data?.[operation.name];
case OperationType.Mutation:
const fetchResponse: FetchResult<any> = await client.mutate({
mutation: gql(operation.graphql),
variables,
});
return fetchResponse.data?.[operation.name];
}
} catch (e) {
if (e instanceof ApolloError) {
const networkError = e.networkError as unknown as NetworkError;
if (networkError?.statusCode == 302) {
window.location.pathname = "/yllapito/kirjaudu";
return;
}
return queryResponse.data?.[operation.name];
case OperationType.Mutation:
const fetchResponse: FetchResult<any> = await client.mutate({
mutation: gql(operation.graphql),
variables,
});
return fetchResponse.data?.[operation.name];
} else {
throw e;
}
}
throw Error("Unknown operation");
}
Expand Down

0 comments on commit 0bd3d95

Please sign in to comment.