I have checked two packages express-session and cookie-parser. I need to find the user's trusted device information. When a user tries to log in from another device. I need to find it. Is it possible to find it like that?
When I used express-session I added a user name to the cookie, when logging in. But I tried to log in with the wrong password I could not get the cookies.
You can use node-device-detector to get the device information on the login and you can use it in a cookie.
const DeviceDetector = require('node-device-detector');
const detector = new DeviceDetector({
clientIndexes: true,
deviceIndexes: true,
deviceAliasCode: false,
});
const userAgent = 'Mozilla/5.0 (Linux; Android 5.0; NX505J Build/KVT49L) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.78 Mobile Safari/537.36';
const result = detector.detect(userAgent);
console.log('result parse', result);
Results:
{
os: {
name: 'Android', // os name
short_name: 'AND', // os short code name (format A-Z0-9{3})
version: '5.0', // os version
platform: '', // os platform (x64, x32, amd etc.)
family: 'Android' // os family
},
client: {
type: 'browser', // client type
name: 'Chrome Mobile', // client name name
short_name: 'CM', // client short code name (only browser, format A-Z0-9{2,3})
version: '43.0.2357.78', // client version
engine: 'Blink', // client engine name (only browser)
engine_version: '' // client engine version (only browser)
family: 'Chrome' // client family (only browser)
},
device: {
id: 'ZT', // short code device brand name (format A-Z0-9{2,3})
type: 'smartphone', // device type
brand: 'ZTE', // device brand name
model: 'Nubia Z7 max' // device model name
code: 'NX505J' // device model code (only result for enable detector.deviceAliasCode)
}
}
You can add cookies like this, you also need a cookie parser:
res.cookie('cookieName', 'addHereYourValues', { expires: new Date(Date.now() + 900000), httpOnly: true })
check here to know more about them.