trulymittal / api-authentication-nodejs Goto Github PK
View Code? Open in Web Editor NEWAPI Authentication using JWT's (JSON Web Tokens). Plug n Play inside any app which requires authentication. NodeJs Express MongoDB & Redis.
API Authentication using JWT's (JSON Web Tokens). Plug n Play inside any app which requires authentication. NodeJs Express MongoDB & Redis.
signRefreshToken: (userId) => {
return new Promise((resolve, reject) => {
const payload = {}
const secret = process.env.REFRESH_TOKEN_SECRET
const options = {
expiresIn: '1y',
issuer: 'pickurpage.com',
audience: userId,
}
JWT.sign(payload, secret, options, (err, token) => {
if (err) {
console.log(err.message)
// reject(err)
reject(createError.InternalServerError())
}
client.SET(userId, token, 'EX', 365 * 24 * 60 * 60, (err, reply) => {
if (err) {
console.log(err.message)
reject(createError.InternalServerError())
return
}
resolve(token)
})
})
})
},
@trulymittal
Hey, Thanks for the tutorial :)
But I've got a question
in User.model.js line: 30
Let's say an error will happen while hashing the password in try
block
It will pass the error to the catch
block and will be passed into next()
function
But where would you handle that error ??
The error is not handled anywhere and it crashes the entire app
userSchema.pre('save', async function (next) {
try {
// something went wrong while hashing the password
throw new Error('something went wrong')
} catch (error) {
// This will crash the app
next(error)
}
})
everything works fine on localhost but when tried to deploy on server getting these errors from redis
Redis connection to 127.0.0.1:6379 failed - connect ECONNREFUSED 127.0.0.1:6379
I have deployed API on another server and Redis on another server and connect each server for Redis but after some time like 10min or so if I try to access Redis data from API it's giving me AbortError: Redis connection lost and command aborted. It might have been processed but on local it's working fine.
I created separate file to test REDIS SET method but sending error of wrong argument.
const client = require('./config/redis_config');
client.SET(1, "test", 'EX', 3 * 24 * 60 * 60, (err, reply) => {
if (err) {
console.log(err);
return;
}
return reply;
});
If I remove EX and TTL it works fine.
github repo
Can you please help me with the client side code that will fetch the /login api and set the Auth header in HttpRequest
Sir how jwt identifies token is expired...?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.