Hi again @hrishikesh, thanks for the response. I was getting a preflight request error, but I managed to solve this issue after reading Access-Control-Allow-Origin Policy. I made an alteration in my function login-user as suggested in the post, and look like this now:
import { client, client_query as q } from '../src/config/db';
import * as jose from 'jose';
import bcrypt from "bcryptjs";
let HEADERS = {
'Access-Control-Allow-Headers': 'Origin, X-Requested-With, Content-Type, Accept, Access-Control-Allow-Origin',
'Content-Type': 'application/json',
'Access-Control-Allow-Methods': 'POST, OPTIONS',
'Access-Control-Max-Age': '8640'
};
HEADERS['Access-Control-Allow-Origin'] = '*';
HEADERS['Vary'] = 'Origin';
export const handler = async (event, context) => {
try {
if (event.httpMethod === 'OPTIONS') {
return {
HEADERS,
statusCode: 200
}
}
if(event.httpMethod = "POST") {
const { email, password, type } = JSON.parse(event.body);
const index =
type == "Profissional"
? "check_user_professional"
: "check_user_client";
const req = await client.query(
q.Map(
q.Paginate(q.Match(q.Index(index), email)),
q.Lambda("user", q.Get(q.Var("user")))
));
const authUser = {
id: req.data[0].ref.id,
...req.data[0].data
}
const validPassword = await bcrypt.compare(password, authUser.password);
if(!validPassword){
return {
HEADERS,
statusCode: 401,
body: JSON.stringify("Login não Autorizado.")
}
}
else{
const secret = new TextEncoder().encode(process.env.JWT_SECRET);
const token = await new jose
.SignJWT({ id: authUser.id, type: authUser.userType })
.setProtectedHeader({ alg: 'HS256' })
.setIssuedAt()
.setExpirationTime('7d')
.sign(secret);
return {
HEADERS,
statusCode: 200,
body: JSON.stringify({
authUser,
token
})
}
}
}
} catch (error) {
return {
HEADERS,
statusCode: 400,
body: JSON.stringify(error)
}
}
}
Just added the headers and changed to a post method. I made the change as you mentioned, using relative URL, like axios.post("api/login-user", ...)
, but I still get an error. This time, the request is failing with status code 400 and I’m getting a ERR_OUT_OF_RANGE in the data response. The error:
If there is anty other information I can provide, please, just let me know.