Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

strapi change user password

module.exports = { 
   index: async ctx => { 
       // Get posted params
       // const params = JSON.parse(ctx.request.body); //if post raw object using Postman
       const params = ctx.request.body;

       // The identifier is required.
       if (!params.identifier) {
          return ctx.badRequest(
             null,
             formatError({
                id: 'Auth.form.error.email.provide',
                message: 'Please provide your username or your e-mail.',
             })
          );
       }

       // Other params validation
       .
       .
       .

       // Get User based on identifier
       const user = await strapi.query('user', 'users-permissions').findOne({username: params.identifier});

       // Validate given password against user query result password
       const validPassword = await strapi.plugins['users-permissions'].services.user.validatePassword(params.password, user.password);
        
       if (!validPassword) {
          return ctx.badRequest(
             null,
             formatError({
                id: 'Auth.form.error.invalid',
                message: 'Identifier or password invalid.',
             })
          );
       } else {
          // Generate new hash password
          const password = await strapi.plugins['users-permissions'].services.user.hashPassword({password: params.newPassword});
          // Update user password
          await strapi
            .query('user', 'users-permissions')
            .update({ id: user.id }, { resetPasswordToken: null, password });

          // Return new jwt token
          ctx.send({
             jwt: strapi.plugins['users-permissions'].services.jwt.issue({ id: user.id }),
             user: sanitizeEntity(user.toJSON ? user.toJSON() : user, { model: strapi.query('user', 'users-permissions').model }),
          }); 
       }
    }
 }
Comment

PREVIOUS NEXT
Code Example
Javascript :: how to sort an array of objects by two fields in javascript 
Javascript :: async await mongoose connection 
Javascript :: react add link to button 
Javascript :: add id attribute to jQuery steps 
Javascript :: javascript wait for dom 
Javascript :: remove double quotes from json array javascript 
Javascript :: js string times 
Javascript :: download a file nodejs 
Javascript :: js get value of input 
Javascript :: react native image auto height 
Javascript :: downgrade node version windows using npm 
Javascript :: why is my req.body empty 
Javascript :: select dropdown value using jquery 
Javascript :: pyspark from_json example 
Javascript :: javascript cors error 
Javascript :: Check if user logged in Wordpress through JS 
Javascript :: js date enlever jour 
Javascript :: mongodb $in regex 
Javascript :: node js example 
Javascript :: javascript group by key 
Javascript :: addclass removeclass jquery 
Javascript :: when i click on one checkbox check all checkboxes 
Javascript :: protected route in react js 
Javascript :: javascript indexof 
Javascript :: express js npm 
Javascript :: url regular expression 
Javascript :: create react native 
Javascript :: react js loop through array of objects 
Javascript :: create a form and submit it dynamically jquery 
Javascript :: js password generator 
ADD CONTENT
Topic
Content
Source link
Name
4+8 =