Can anyone please help me understand the differenc...
# random
j
Can anyone please help me understand the difference between these two functions? Why does one take context and one takes request? (Sorry if this isn't a prisma specific question - if I need to ask for help in a more appropriate place, please let me know & my apologies in advance):
function 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");
// }
j
I have a question. what graphql server you are using?
Apollo graphql server?
the different of two functions
function 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 directly
j
Yes, I'm using Apollo, thanks for pointing me in the right direction @Jarupong
I'm going to read the documentation, but any pointers you have to get this working is much appreciated
j
Copy code
req.headers.authorization;
It because you don't pass authorization headers
j
How do I pass it? (sorry I"m a total beginner)
j
You should pass it from client side
are you using apollo client?
j
Yes!
Let me try to find my code and paste it here
you can check here
by pass you authorization string
j
I have
const authLink = setContext((_, { headers }) => {
const token = localStorage.getItem(AUTH_TOKEN);
return {
headers: {
...headers,
`authorization: token ?
Bearer ${token}
: "",
Copy code
},
Copy code
};`
});
// 3
const client = new ApolloClient({
link: authLink.concat(httpLink),
cache: new InMemoryCache(),
});
Do I need something in addition? This is what it says in the documentation
j
please check token is it existing or not
j
I have
const authToken = localStorage.getItem(AUTH_TOKEN);
console.log(authToken);
And I get
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOjUsImlhdCI6MTY2Njg0MTM5Mn0.SYwxmxi90X2ByB4GzpcZg3nsrVDuJjBatBqzxllyIQs
Can I use that to access the current user somehow?
j
you can use
decode()
from jsonwebtoken module
j
Cool I'll try that
Any ideas on how to get the ME_QUERY to work to display the current user? Just so I can try to understand how it works
I'm feeling concerend about using Prisma instead of postgres directly, because I"m nervous I won't understand how to get to the data very well... any thoughts on that?
Thanks again for all your help
j
Copy code
Any 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_QUERY
Copy code
I'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)
j
I don't have much experience with ORMs & I want to make a very basic slack clone with authentication just to practice (I want to understnad how database, front end and back end all connect to each other)
Most tutorials I see use an ORM so I figured I'd do it
j
Do you have any experience with SQL before?
j
But I have a background in statistical analysis so I'd love to be able to add some analytics to the project, like show what users are the most active, statistics around their usage
I think I'm a little more comfortable with SQL than with ORMs
j
It seems like your background is answer for SQL instead of ORMs because data analytics, reporting need to write complex queries to get data
But If you need to learn ORMs and writing complex SQL at the same time, Prisma support to write raw SQL query https://www.prisma.io/docs/concepts/components/prisma-client/raw-database-access
j
Ok cool, thank you. I think I'm going to take a step back and try the project again starting with sql, since for now I am a little more comfortable with that
I may try orms again in the future
j
Sure thing. If you need help about Prisma you can ask #orm-help 😄
❤️ 1