John Bradens
10/27/2022, 3:07 AMfunction getUserId(context) {
const Authorization = context.request.get("Authorization");
console.log("hello");
console.log(context.request);
if (Authorization) {
const token = Authorization.replace("Bearer ", "");
const { userId } = jwt.verify(token, process.env.APP_SECRET);
return userId;
}
throw new AuthError();
}
class AuthError extends Error {
constructor() {
super("Not authorized");
}
}
module.exports = {
getUserId,
AuthError,
APP_SECRET,
};
// function getUserId(req, authToken) {
// if (req) {
// const authHeader = req.headers.authorization;
// if (authHeader) {
// const token = authHeader.replace("Bearer ", "");
// if (!token) {
// throw new Error("No token found");
// }
// const { userId } = getTokenPayload(token);
// return userId;
// }
// } else if (authToken) {
// const { userId } = getTokenPayload(authToken);
// return userId;
// }
// throw new Error("Not authenticated");
// }
Jarupong
10/27/2022, 3:10 AMJarupong
10/27/2022, 3:11 AMJarupong
10/27/2022, 3:11 AMJarupong
10/27/2022, 3:11 AMJarupong
10/27/2022, 3:13 AMfunction getUserId(context)
is used to get authorization header from graphql server context. In contrast function getUserId(req, authToken)
is used to get authorization header from request directlyJohn Bradens
10/27/2022, 3:21 AMJohn Bradens
10/27/2022, 3:21 AMJohn Bradens
10/27/2022, 3:22 AMJarupong
10/27/2022, 3:22 AMreq.headers.authorization;
It because you don't pass authorization headersJohn Bradens
10/27/2022, 3:23 AMJarupong
10/27/2022, 3:23 AMJarupong
10/27/2022, 3:24 AMJohn Bradens
10/27/2022, 3:24 AMJohn Bradens
10/27/2022, 3:24 AMJarupong
10/27/2022, 3:24 AMJarupong
10/27/2022, 3:24 AMJarupong
10/27/2022, 3:24 AMJohn Bradens
10/27/2022, 3:25 AMconst authLink = setContext((_, { headers }) => {
const token = localStorage.getItem(AUTH_TOKEN);
return {
headers: {
...headers,
`authorization: token ? Bearer ${token}
: "",
},
};`
});
// 3
const client = new ApolloClient({
link: authLink.concat(httpLink),
cache: new InMemoryCache(),
});
John Bradens
10/27/2022, 3:26 AMJarupong
10/27/2022, 3:26 AMJohn Bradens
10/27/2022, 3:32 AMconst authToken = localStorage.getItem(AUTH_TOKEN);
console.log(authToken);
John Bradens
10/27/2022, 3:32 AMeyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOjUsImlhdCI6MTY2Njg0MTM5Mn0.SYwxmxi90X2ByB4GzpcZg3nsrVDuJjBatBqzxllyIQs
John Bradens
10/27/2022, 3:32 AMJarupong
10/27/2022, 3:34 AMJarupong
10/27/2022, 3:34 AMdecode()
from jsonwebtoken moduleJarupong
10/27/2022, 3:35 AMJohn Bradens
10/27/2022, 3:36 AMJohn Bradens
10/27/2022, 3:37 AMJohn Bradens
10/27/2022, 3:37 AMJohn Bradens
10/27/2022, 3:37 AMJarupong
10/27/2022, 3:39 AMAny ideas on how to get the ME_QUERY to work to display the current user?
You can use authorization headers to verify and decode userId from backend side then you get ME_QUERYJarupong
10/27/2022, 3:41 AMI'm feeling concerend about using Prisma instead of postgres directly
I have two questions
• Do you have any experience with ORM library before?
• Is your project complicated? I mean to write SQL query or just simple CRUD(Create, Read, Update, Delete)John Bradens
10/27/2022, 3:42 AMJohn Bradens
10/27/2022, 3:42 AMJarupong
10/27/2022, 3:43 AMJohn Bradens
10/27/2022, 3:43 AMJohn Bradens
10/27/2022, 3:43 AMJarupong
10/27/2022, 3:44 AMJarupong
10/27/2022, 3:45 AMJohn Bradens
10/27/2022, 3:45 AMJohn Bradens
10/27/2022, 3:46 AMJarupong
10/27/2022, 3:49 AM