System Information
- v4.25.5:
- linux ubuntu:
- postgres:
- Node Version 18:
Hello everyone! Cannt create a custom route for user-permissions:
extensions/users-permissions/controllers/auth.js
'use strict';
const { sanitizeEntity } = require('strapi-utils');
module.exports = {
async googleDoctor(ctx) {
const { id_token } = ctx.query;
if (!id_token) {
return ctx.badRequest('Missing ID token');
}
// Verify ID token with Google
const ticket = await strapi.plugins['users-permissions'].services.jwt.verifyGoogleToken(id_token);
const { email, name } = ticket.getPayload();
// Check if user already exists
let user = await strapi.query('plugin::users-permissions.user').findOne({ where: { email } });
if (!user) {
// Assign doctor role
const role = await strapi.query('plugin::users-permissions.role').findOne({ where: { type: 'doctor' } });
if (!role) {
return ctx.badRequest('Doctor role not found');
}
user = await strapi.plugins['users-permissions'].services.user.add({
email,
username: name,
confirmed: true,
provider: 'google',
role: role.id,
});
} else {
// If user exists, check if the role is doctor
const role = await strapi.query('plugin::users-permissions.role').findOne({ where: { id: user.role } });
if (role.type !== 'doctor') {
return ctx.badRequest('User is not a doctor');
}
}
// Generate JWT token
const jwt = strapi.plugins['users-permissions'].services.jwt.issue({ id: user.id });
ctx.send({
jwt,
user: sanitizeEntity(user, { model: strapi.plugins['users-permissions'].models.user }),
});
},
async registerDoctor(ctx) {
const { email, password, fullName } = ctx.request.body;
if (!email || !password || !fullName) {
return ctx.badRequest('Please provide email, password, and full name');
}
const existingUser = await strapi.query('plugin::users-permissions.user').findOne({ where: { email } });
if (existingUser) {
return ctx.badRequest('Email is already taken');
}
const role = await strapi.query('plugin::users-permissions.role').findOne({ where: { type: 'doctor' } });
if (!role) {
return ctx.badRequest('Doctor role not found');
}
const newUser = await strapi.plugins['users-permissions'].services.user.add({
email,
password,
username: fullName,
confirmed: true,
role: role.id,
});
ctx.send(sanitizeEntity(newUser, { model: strapi.plugins['users-permissions'].models.user }));
},
};
controllers/register-doctor.js:
'use strict';
const { sanitizeEntity } = require('strapi-utils');
module.exports = {
async registerDoctor(ctx) {
const { email, password, fullName } = ctx.request.body;
if (!email || !password || !fullName) {
return ctx.badRequest('Please provide email, password, and full name');
}
const existingUser = await strapi.query('plugin::users-permissions.user').findOne({ where: { email } });
if (existingUser) {
return ctx.badRequest('Email is already taken');
}
const role = await strapi.query('plugin::users-permissions.role').findOne({ where: { type: 'doctor' } });
if (!role) {
return ctx.badRequest('Doctor role not found');
}
const newUser = await strapi.plugins['users-permissions'].services.user.add({
email,
password,
username: fullName,
confirmed: true,
role: role.id,
});
ctx.send(sanitizeEntity(newUser, { model: strapi.plugins['users-permissions'].models.user }));
},
};
routes/routes.js:
module.exports = {
"routes": [
{
"method": "GET",
"path": "/auth/google-doctor",
"handler": "auth.googleDoctor",
"config": {
"policies": []
}
},
{
"method": "POST",
"path": "/auth/register-doctor",
"handler": "auth.registerDoctor",
"config": {
"policies": []
}
}
]
}
services/jwt.js
const { OAuth2Client } = require('google-auth-library');
const client = new OAuth2Client(process.env.GOOGLE_CLIENT_ID);
module.exports = {
async verifyGoogleToken(idToken) {
const ticket = await client.verifyIdToken({
idToken,
audience: process.env.GOOGLE_CLIENT_ID,
});
return ticket;
},
};
2 roots via google authentication, the first one using google to create a user with the role “doctor” (standard role “patient”), and the second root just creating a user with the role “doctor”. but Strapi does not see these roots - what is the reason?