fix: enable cors on login/logout api

This commit is contained in:
MTG2000
2022-06-05 21:11:49 +03:00
parent c9845b28da
commit 41b72cf6ad
10 changed files with 30 additions and 14 deletions

View File

@@ -29,7 +29,7 @@ const server = new ApolloServer({
const apolloHandler = server.createHandler({
expressGetMiddlewareOptions: {
cors: {
origin: true,
origin: 'http://localhost:3000',
credentials: true,
}
},

View File

@@ -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' })
}

View File

@@ -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
}
}
};

View File

@@ -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;

View File

@@ -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";

View File

@@ -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() {

View File

@@ -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(

View File

@@ -0,0 +1,5 @@
const CONSTS = {
apiEndpoint: 'http://localhost:8888/.netlify/functions'
}
export default CONSTS;

View File

@@ -1,5 +0,0 @@
const CONSTS = {
apiEndpoint: process.env.REACT_APP_API_END_POINT ?? '/.netlify/functions'
}
export default CONSTS;

1
src/utils/index.ts Normal file
View File

@@ -0,0 +1 @@
export { default as CONSTS } from './consts/consts'