DEV Community

FredAbod
FredAbod

Posted on

6

Authorization In Node.js, All You Need To Know

Liquid syntax error: 'raw' tag was never closed

Sentry blog image

How I fixed 20 seconds of lag for every user in just 20 minutes.

Our AI agent was running 10-20 seconds slower than it should, impacting both our own developers and our early adopters. See how I used Sentry Profiling to fix it in record time.

Read more

Top comments (2)

Collapse
 
joelbonetr profile image
JoelBonetR πŸ₯‡ β€’

Hi @fredabod , nice introduction to this topic.

Pointing out improvement points:

Assuming that the headers look like that:

Authorization: {
    token: 'myToken'
}
Enter fullscreen mode Exit fullscreen mode

I'd change

const token = req.headers.authorization.split(' ')[1];
Enter fullscreen mode Exit fullscreen mode

for

const { token } = req.headers.authorization;
Enter fullscreen mode Exit fullscreen mode

just to avoid runtime errors if something went wrong (e.g. Authorization being undefined) as well as for maintainability as you can know what it does by just reading at this while in the array hardcoded position implementation you can be wondering "what the heck does Authorization have?".

Also take a look at CORS and specially to the DB Salt concept so you get better security on this.

Hope it helps somehow, best regards 😁

Collapse
 
fredabod profile image
FredAbod β€’

This was really helpful. Thank you @joelbonetr

nextjs tutorial video

Youtube Tutorial Series πŸ“Ί

So you built a Next.js app, but you need a clear view of the entire operation flow to be able to identify performance bottlenecks before you launch. But how do you get started? Get the essentials on tracing for Next.js from @nikolovlazar in this video series πŸ‘€

Watch the Youtube series