strapi permanent tokens
strapi does not support permanent tokens out of the box. You may need to use permanent tokens for devops integration.
Modify this file /plugins/users-permissions/services/Jwt.js to make strapi support permanent tokens.
diff --git a/plugins/users-permissions/services/Jwt.js b/plugins/users-permissions/services/Jwt.js
index f59855de094399e2674fbea2bf83407f90483983..80a5ec23aa231f35130ed43e95e487dea80546ee 100644
--- a/plugins/users-permissions/services/Jwt.js
+++ b/plugins/users-permissions/services/Jwt.js
@@ -38,8 +38,10 @@ module.exports = {
return this.verify(token);
},
- issue: (payload, jwtOptions = {}) => {
- _.defaults(jwtOptions, defaultJwtOptions);
+ issue: (payload, jwtOptions = {}, useDefault = true) => {
+ if (useDefault) {
+ _.defaults(jwtOptions, defaultJwtOptions);
+ }
return jwt.sign(
_.clone(payload.toJSON ? payload.toJSON() : payload),
process.env.JWT_SECRET || _.get(strapi.plugins['users-permissions'], 'config.jwtSecret') || 'oursecret',
@@ -61,5 +63,30 @@ module.exports = {
}
);
});
+ },
+
+ // This method is to be used in strapi console
+ generatePermanentToken: async (identifier) => {
+ const emailRegExp = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
+ const query = {};
+
+ // Check if the provided identifier is an email or not.
+ const isEmail = emailRegExp.test(identifier);
+
+ // Set the identifier to the appropriate query field.
+ if (isEmail) {
+ query.email = identifier.toLowerCase();
+ } else {
+ query.username = identifier;
+ }
+
+ const users = await strapi.plugins['users-permissions'].services.user.fetchAll(query);
+ if (users.length == 0) {
+ console.log('Cannot find the user');
+ } else {
+ let user = users[0];
+ let token = strapi.plugins['users-permissions'].services.jwt.issue(_.pick(user.toJSON ? user.toJSON() : user, ['_id', 'id']), {}, false);
+ console.log(`Token is ${token}`);
+ }
}
-};
+};
\ No newline at end of file
Use generatePermanentToken method to generate a permanent token in strapi console.