All files / store auth.js

0% Statements 0/33
0% Branches 0/12
0% Functions 0/6
0% Lines 0/32

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152                                                                                                                                                                                                                                                                                                               
import { getValidator } from './index'
 
export const store = () => ({
  username: '',
  loggedIn: false,
})
 
// TODO: passwordStrengthConstraint using https://www.npmjs.com/package/password-validator
 
const passwordConfirmationConstraint = {
  field: 'passwordConfirmation',
  message: 'Les deux mot de passe ne correspondent pas',
  constraint(_, object) {
    const pwd1 = object.password
    const pwd2 = object.passwordConfirmation
    return pwd1 === pwd2
  },
}
 
export const getters = {
  validateLogin: getValidator({
    resourceName: { gender: 'M', name: 'compte' },
    fieldNames: {
      username: { gender: 'M', name: "nom d'utilisateur" },
      password: { gender: 'M', name: 'mot de passe' },
    },
    constraints: {
      notEmpty: ['username', 'password'],
      maxLength: {
        300: ['username'],
      },
    },
  }),
  validateRegister: getValidator({
    resourceName: { gender: 'M', name: 'compte' },
    fieldNames: {
      username: { gender: 'M', name: "nom d'utilisateur" },
      email: { gender: 'F', name: 'adresse email' },
      password: { gender: 'M', name: 'mot de passe' },
      passwordConfirmation: {
        gender: 'F',
        name: 'confirmation du mot de passe',
      },
    },
    constraints: {
      notEmpty: ['username', 'password', 'email'],
      maxLength: {
        300: ['username', 'email'],
      },
      isAnEmail: ['email'],
    },
    customConstraints: [passwordConfirmationConstraint],
  }),
  validatePasswordReset: getValidator({
    resourceName: { gender: 'M', name: 'mot de passe' },
    fieldNames: {
      password: { gender: 'M', name: 'mot de passe' },
      passwordConfirmation: {
        gender: 'F',
        name: 'confirmation du mot de passe',
      },
      email: { gender: 'F', name: 'adresse email' },
    },
    constraints: {
      required: ['password'],
      isAnEmail: ['email'],
      maxLength: {
        300: ['email'],
      },
    },
    customConstraints: [passwordConfirmationConstraint],
    debug: true,
  }),
  validateEmailAdress: getValidator({
    resourceName: { gender: 'M', name: 'addresse email' },
    fieldNames: {
      email: { gender: 'F', name: 'adresse email' },
    },
    constraints: {
      required: ['email'],
      isAnEmail: ['email'],
    },
  }),
}
 
export const actions = {
  async login({ commit }, data) {
    try {
      await this.$auth.loginWith('local', { data })
      this.$router.push('/')
    } catch (e) {
      if (
        e.response.data.non_field_errors &&
        e.response.data.non_field_errors[0] ===
          'Unable to log in with provided credentials.'
      ) {
        this.$toast.error("Mot de passe ou nom d'utilisateur incorrect", {
          icon: 'error_outline',
        })
        return false
      }
      this.$toast.error('Erreur interne, veuillez réessayer plus tard.', {
        icon: 'error_outline',
      })
    }
  },
  async register({ dispatch, getters }, data) {
    try {
      await this.$axios.post('/users/', data)
      return true
    } catch (error) {
      const data = error.response.data
      if (
        data.email &&
        data.email[0] === 'user with this email already exists.'
      ) {
        this.$toast.error('Cette adresse e-mail est déjà prise', {
          icon: 'error_outline',
        })
      } else if (
        data.username &&
        data.username[0] === 'A user with that username already exists.'
      ) {
        this.$toast.error("Ce nom d'utilisateur est déjà pris", {
          icon: 'error_outline',
        })
      } else {
        this.$toast.error('Erreur lors de la création du compte', {
          icon: 'error_outline',
        })
      }
      return false
    }
  },
  async requestPasswordReset(_, data) {
    try {
      const res = await this.$axios.post('/password-reset/', data)
      return res.data.status === 'OK'
    } catch (error) {
      return false
    }
  },
  async changePassword(_, data) {
    try {
      const res = await this.$axios.post('/password-reset/confirm/', data)
      return res.data.status === 'OK'
    } catch (error) {
      return false
    }
  },
}