Skip to content

Commit

Permalink
Merge pull request #1269 from EcrituresNumeriques/feat/1147
Browse files Browse the repository at this point in the history
Mémorise la date de dernière connexion
  • Loading branch information
thom4parisot authored Feb 12, 2025
2 parents 8572e6e + d161dc9 commit ca27b8b
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 71 deletions.
13 changes: 6 additions & 7 deletions graphql/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,8 +244,8 @@ app.get(
'/login/openid',
async (req, res, next) => {
if (req.user) {
const { email } = req.user
const token = await createJWTToken({ email, jwtSecret })
const user = await User.assessLogin({ email: req.user.email })
const token = await createJWTToken({ user, jwtSecret })
res.redirect(`${req.headers.referer ?? '/'}#auth-token=${token}`)
} else {
req.session.origin = req.headers.referer
Expand Down Expand Up @@ -302,8 +302,8 @@ app.use(
'/authorization-code/callback',
passport.authenticate('oidc', { failWithError: true }),
async function onSuccess(req, res) {
const { email } = req.user
const token = await createJWTToken({ email, jwtSecret })
const user = await User.assessLogin({ email: req.user.email })
const token = await createJWTToken({ user, jwtSecret })
return res.redirect(`${req.session.origin ?? '/'}#auth-token=${token}`)
},
function onFailure(error, req, res) {
Expand All @@ -326,9 +326,8 @@ app.post(
'/login/local',
passport.authenticate('local', { failWithError: true }),
async function onSuccess(req, res) {
const { email } = req.user

const token = await createJWTToken({ email, jwtSecret })
const user = await User.assessLogin({ email: req.user.email })
const token = await createJWTToken({ user, jwtSecret })

res.statusCode = 200
res.json({ user: req.user, token })
Expand Down
13 changes: 8 additions & 5 deletions graphql/helpers/token.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ const jwt = require('jsonwebtoken')
const User = require('../models/user')
const Sentry = require('@sentry/node')

module.exports.createJWTToken = async function createJWTToken ({ email, jwtSecret}) {
const user = await User.findOne({ email })

module.exports.createJWTToken = async function createJWTToken({
user,
jwtSecret,
}) {
// generate a JWT token
const payload = {
email,
email: user.email,
_id: user._id,
authType: user.authType,
admin: Boolean(user.admin),
Expand All @@ -17,7 +18,9 @@ module.exports.createJWTToken = async function createJWTToken ({ email, jwtSecre
return jwt.sign(payload, jwtSecret)
}

module.exports.populateUserFromJWT = function populateUserFromJWT ({ jwtSecret }) {
module.exports.populateUserFromJWT = function populateUserFromJWT({
jwtSecret,
}) {
return async function populateUserFromJWTMiddleware(req, res, next) {
const jwtToken = req.headers.authorization?.replace(/^Bearer\s+/, '')

Expand Down
110 changes: 51 additions & 59 deletions graphql/models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,67 +5,59 @@ const { article: defaultArticle } = require('../data/defaultsData')

const Schema = mongoose.Schema

const userSchema = new Schema({
email: {
type: String,
unique: true,
required: true
},
username: {
type: String,
const userSchema = new Schema(
{
email: {
type: String,
unique: true,
required: true,
},
displayName: String,
// unique but not required, we need to create a sparse index manually
username: String,
// TODO remove this link
tags: [
{
type: Schema.Types.ObjectId,
ref: 'Tag',
},
],
acquintances: [
{
type: Schema.Types.ObjectId,
ref: 'User',
},
],
articles: [
{
type: Schema.Types.ObjectId,
ref: 'Article',
},
],
authType: {
type: String,
default: 'local',
enum: ['local', 'oidc'],
},
password: {
type: String,
default: null,
set: (password) => {
return bcrypt.hashSync(password, 10)
},
},
admin: {
type: Boolean,
default: false,
},
firstName: String,
lastName: String,
institution: String,
connectedAt: Date,
zoteroToken: String,
},
// TODO remove this link
tags: [
{
type: Schema.Types.ObjectId,
ref: 'Tag'
}
],
acquintances: [
{
type: Schema.Types.ObjectId,
ref: 'User'
}
],
articles: [
{
type: Schema.Types.ObjectId,
ref: 'Article'
}
],
authType: {
type: String,
default: 'local',
enum: ['local', 'oidc']
},
password: {
type: String,
default: null,
set: (password) => {
return bcrypt.hashSync(password, 10)
}
},
displayName: {
type: String,
},
admin: {
type: Boolean,
default: false
},
firstName: {
type: String
},
lastName: {
type: String
},
institution: {
type: String
},
zoteroToken: {
type: String
}
}, { timestamps: true })
{ timestamps: true }
)

/**
* Compare an existing password against a user input one.
Expand Down

0 comments on commit ca27b8b

Please sign in to comment.