From 41b72cf6ad23eec2067dae36586606cd13cfc03b Mon Sep 17 00:00:00 2001 From: MTG2000 Date: Sun, 5 Jun 2022 21:11:49 +0300 Subject: [PATCH] fix: enable cors on login/logout api --- functions/graphql/index.js | 2 +- functions/login/login.js | 6 ++++++ functions/logout/logout.js | 5 ++++- functions/utils/consts.js | 10 +++++++++- src/features/Auth/pages/LoginPage/LoginPage.tsx | 2 +- src/features/Auth/pages/LogoutPage/LogoutPage.tsx | 2 +- src/utils/apollo.ts | 6 ++---- src/utils/consts/consts.ts | 5 +++++ src/utils/consts/index.ts | 5 ----- src/utils/index.ts | 1 + 10 files changed, 30 insertions(+), 14 deletions(-) create mode 100644 src/utils/consts/consts.ts delete mode 100644 src/utils/consts/index.ts create mode 100644 src/utils/index.ts diff --git a/functions/graphql/index.js b/functions/graphql/index.js index 91e00a7..0ac8860 100644 --- a/functions/graphql/index.js +++ b/functions/graphql/index.js @@ -29,7 +29,7 @@ const server = new ApolloServer({ const apolloHandler = server.createHandler({ expressGetMiddlewareOptions: { cors: { - origin: true, + origin: 'http://localhost:3000', credentials: true, } }, diff --git a/functions/login/login.js b/functions/login/login.js index 3dd7238..8bcc145 100644 --- a/functions/login/login.js +++ b/functions/login/login.js @@ -4,12 +4,14 @@ const LnurlService = require('../auth/services/lnurl.service') const cookie = require('cookie') const jose = require('jose'); const { CONSTS } = require('../utils'); +const { CORS_HEADERS } = require('../utils/consts'); async function generateAuthUrl() { const data = await LnurlService.generateAuthUrl(); return { statusCode: 200, + headers: CORS_HEADERS, body: JSON.stringify(data) }; } @@ -19,6 +21,7 @@ async function login(tag, k1, sig, key) { if (tag !== 'login') { return { statusCode: 400, + CORS_HEADERS, body: JSON.stringify({ status: 'ERROR', reason: 'Not a login request' }) } } @@ -28,6 +31,7 @@ async function login(tag, k1, sig, key) { } catch (error) { return { statusCode: 400, + CORS_HEADERS, body: JSON.stringify({ status: 'ERROR', reason: 'Invalid Signature' }) } } @@ -77,6 +81,7 @@ async function login(tag, k1, sig, key) { 'headers': { 'Set-Cookie': authCookie, 'Cache-Control': 'no-cache', + ...CORS_HEADERS }, body: JSON.stringify({ status: 'OK', @@ -85,6 +90,7 @@ async function login(tag, k1, sig, key) { } catch (error) { return { statusCode: 200, + headers: CORS_HEADERS, body: JSON.stringify({ status: 'ERROR', reason: 'Unexpected error happened, please try again' }) } diff --git a/functions/logout/logout.js b/functions/logout/logout.js index 58f66a6..53082a9 100644 --- a/functions/logout/logout.js +++ b/functions/logout/logout.js @@ -1,5 +1,6 @@ -const cookie = require('cookie') +const cookie = require('cookie'); +const { CORS_HEADERS } = require('../utils/consts'); exports.handler = async (event, context) => { const myCookie = cookie.serialize('Authorization', '', { @@ -10,12 +11,14 @@ exports.handler = async (event, context) => { }) return { statusCode: 200, + body: JSON.stringify({ status: 'OK', }), 'headers': { 'Set-Cookie': myCookie, 'Cache-Control': 'no-cache', + ...CORS_HEADERS } } }; \ No newline at end of file diff --git a/functions/utils/consts.js b/functions/utils/consts.js index c6f007a..1aadfb1 100644 --- a/functions/utils/consts.js +++ b/functions/utils/consts.js @@ -2,10 +2,18 @@ const BOLT_FUN_LIGHTNING_ADDRESS = 'johns@getalby.com'; // #TODO, replace it by const JWT_SECRET = process.env.JWT_SECRET; const LNURL_AUTH_HOST = process.env.LNURL_AUTH_HOST +const CORS_HEADERS = { + 'Access-Control-Allow-Origin': 'http://localhost:3000', + 'Access-Control-Allow-Headers': 'Content-Type', + 'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE', + 'Access-Control-Allow-Credentials': true +}; + const CONSTS = { JWT_SECRET, BOLT_FUN_LIGHTNING_ADDRESS, - LNURL_AUTH_HOST + LNURL_AUTH_HOST, + CORS_HEADERS } module.exports = CONSTS; \ No newline at end of file diff --git a/src/features/Auth/pages/LoginPage/LoginPage.tsx b/src/features/Auth/pages/LoginPage/LoginPage.tsx index 74aa682..e3017d5 100644 --- a/src/features/Auth/pages/LoginPage/LoginPage.tsx +++ b/src/features/Auth/pages/LoginPage/LoginPage.tsx @@ -3,7 +3,7 @@ import { BsFillLightningChargeFill } from "react-icons/bs"; import { Grid } from "react-loader-spinner"; import { useNavigate } from "react-router-dom"; import { useMeQuery } from "src/graphql" -import CONSTS from "src/utils/consts"; +import { CONSTS } from "src/utils"; diff --git a/src/features/Auth/pages/LogoutPage/LogoutPage.tsx b/src/features/Auth/pages/LogoutPage/LogoutPage.tsx index 076f3ce..2414a93 100644 --- a/src/features/Auth/pages/LogoutPage/LogoutPage.tsx +++ b/src/features/Auth/pages/LogoutPage/LogoutPage.tsx @@ -1,7 +1,7 @@ import { useEffect } from "react" import { LineWave } from "react-loader-spinner"; import { useNavigate } from "react-router-dom"; -import CONSTS from "src/utils/consts"; +import { CONSTS } from "src/utils"; export default function LoginPage() { diff --git a/src/utils/apollo.ts b/src/utils/apollo.ts index c7f47d4..9640415 100644 --- a/src/utils/apollo.ts +++ b/src/utils/apollo.ts @@ -1,19 +1,17 @@ import { ApolloClient, HttpLink, InMemoryCache, from, Reference, FieldPolicy } from "@apollo/client"; import { onError } from "@apollo/client/link/error"; import { RetryLink } from "@apollo/client/link/retry"; -import CONSTS from "./consts"; +import { CONSTS } from "src/utils"; let apiClientUri = CONSTS.apiEndpoint + '/graphql'; const httpLink = new HttpLink({ uri: apiClientUri, - credentials: process.env.REACT_APP_API_END_POINT?.includes('localhost') ? 'include' : "same-origin" + credentials: "include" }); const errorLink = onError(({ graphQLErrors, networkError, response }) => { - console.log('AHIHIHA'); - if (graphQLErrors) graphQLErrors.forEach(({ message, locations, path }) => console.log( diff --git a/src/utils/consts/consts.ts b/src/utils/consts/consts.ts new file mode 100644 index 0000000..3c1358a --- /dev/null +++ b/src/utils/consts/consts.ts @@ -0,0 +1,5 @@ +const CONSTS = { + apiEndpoint: 'http://localhost:8888/.netlify/functions' +} + +export default CONSTS; \ No newline at end of file diff --git a/src/utils/consts/index.ts b/src/utils/consts/index.ts deleted file mode 100644 index 558a277..0000000 --- a/src/utils/consts/index.ts +++ /dev/null @@ -1,5 +0,0 @@ -const CONSTS = { - apiEndpoint: process.env.REACT_APP_API_END_POINT ?? '/.netlify/functions' -} - -export default CONSTS; \ No newline at end of file diff --git a/src/utils/index.ts b/src/utils/index.ts new file mode 100644 index 0000000..86c84ae --- /dev/null +++ b/src/utils/index.ts @@ -0,0 +1 @@ +export { default as CONSTS } from './consts/consts' \ No newline at end of file