Browse Source

Display registration errors to user

master
Immanuel Onyeka 1 year ago
parent
commit
846034c11a
4 changed files with 34 additions and 13 deletions
  1. +13
    -3
      grav-admin/user/js/registration/account.vue
  2. +9
    -2
      grav-admin/user/js/registration/registration.vue
  3. +1
    -0
      migrations/0_29092022_setup_tables.sql
  4. +11
    -8
      skouter.go

+ 13
- 3
grav-admin/user/js/registration/account.vue View File

@@ -15,9 +15,19 @@
<input type="email" name="email" v-model="user.email"/> <input type="email" name="email" v-model="user.email"/>
</div> </div>
<div>
<label>Title</label>
<select name="country" v-model="user.title">
<option value="Loan Officer">Loan Officer</option>
<option value="Mortgage Broker">Mortgage Broker</option>
<option value="Executive">Executive</option>
<option value="Other">Other</option>
</select>
</div>
<div> <div>
<label>Password</label> <label>Password</label>
<input requried type="password" name="pass" v-model="user.pass"/>
<input requried type="password" name="pass" v-model="user.password"/>
</div> </div>
<div> <div>
@@ -41,7 +51,7 @@


<div><label>NMLS ID</label><input type="text" name="nmls"/></div> <div><label>NMLS ID</label><input type="text" name="nmls"/></div>
<div><span class="error">{{error}}</span></div>
<div><span class="error">{{err}}</span></div>
<div> <div>
<button class="btn" @click="submit" type="button">Continue</button> <button class="btn" @click="submit" type="button">Continue</button>
@@ -57,7 +67,7 @@ const addresses = ref([])
const user = ref({}) const user = ref({})
const address = ref({}) const address = ref({})
const locationsId = ref(null) const locationsId = ref(null)
const props = defineProps(['error'])
const props = defineProps(['err'])
const emit = defineEmits(['submit']) const emit = defineEmits(['submit'])


function searchLocation(e) { function searchLocation(e) {


+ 9
- 2
grav-admin/user/js/registration/registration.vue View File

@@ -1,7 +1,7 @@
<template> <template>
<section class="shadowbox"> <section class="shadowbox">
<h2>Register</h2> <h2>Register</h2>
<account :error="''" @submit="create" />
<account v-if="!step" :err="err" @submit="create" />
</section> </section>
</template> </template>


@@ -9,6 +9,9 @@
import { ref } from "vue" import { ref } from "vue"
import Account from "./account.vue" import Account from "./account.vue"


let err = ref("")
const step = ref(0)

function create(user) { function create(user) {
console.log(user) console.log(user)
fetch(`/api/user`, fetch(`/api/user`,
@@ -19,7 +22,11 @@ function create(user) {
}, },
}).then(resp => { }).then(resp => {
console.log(resp) console.log(resp)
if (resp.ok) { return resp.json() } else { return resp.text() }
if (resp.ok) {
return resp.json()
} else {
resp.text().then( e => err.value = e)
}
}).then(u => console.log(u)) }).then(u => console.log(u))
} }
</script> </script>


+ 1
- 0
migrations/0_29092022_setup_tables.sql View File

@@ -41,6 +41,7 @@ CREATE TABLE user (
title ENUM('Loan Officer', title ENUM('Loan Officer',
'Branch Manager', 'Branch Manager',
'Mortgage Broker', 'Mortgage Broker',
'Executive',
'Other') NOT NULL, 'Other') NOT NULL,
status ENUM('Trial', status ENUM('Trial',
'Free', 'Free',


+ 11
- 8
skouter.go View File

@@ -1397,29 +1397,30 @@ func deleteUser(w http.ResponseWriter, db *sql.DB, r *http.Request) {


// Checks if a user's entries are reasonable before database insertion. // Checks if a user's entries are reasonable before database insertion.
// This function is very important because it is the only thing preventing // This function is very important because it is the only thing preventing
// anyone from creating an admin user.
// anyone from creating an admin user. These error messages are displayed to
// the user.
func (user *User) validate() error { func (user *User) validate() error {
_, err := mail.ParseAddress(user.Email) _, err := mail.ParseAddress(user.Email)
if err != nil { errors.New("Invalid email.") }
if err != nil { return errors.New("Invalid email.") }
if roles[user.Role] == 0 { if roles[user.Role] == 0 {
errors.New("Invalid role.")
return errors.New("Invalid role.")
} }
if roles[user.Role] == roles["Admin"] { if roles[user.Role] == roles["Admin"] {
errors.New("New user cannot be an Admin.")
return errors.New("New user cannot be an Admin.")
} }
if user.FirstName == "" { if user.FirstName == "" {
errors.New("Given name cannot be empty.")
return errors.New("Given name cannot be empty.")
} }
if user.LastName == "" { if user.LastName == "" {
errors.New("Surname cannot be empty.")
return errors.New("Surname cannot be empty.")
} }
if user.Password == "" { if user.Password == "" {
errors.New("User must have a password.")
return errors.New("Empty password")
} }
return nil return nil
@@ -1430,11 +1431,13 @@ func createUser(w http.ResponseWriter, db *sql.DB, r *http.Request) {
err := json.NewDecoder(r.Body).Decode(&user) err := json.NewDecoder(r.Body).Decode(&user)
if err != nil { http.Error(w, "Invalid fields.", 422); return } if err != nil { http.Error(w, "Invalid fields.", 422); return }
user.Role = "User"
user.Status = "Trial"
err = user.validate() err = user.validate()
if err != nil { http.Error(w, err.Error(), 422); return } if err != nil { http.Error(w, err.Error(), 422); return }


user, err = insertUser(db, user) user, err = insertUser(db, user)
if err != nil { http.Error(w, "Error creating user.", 422); return }
if err != nil { http.Error(w, err.Error(), 422); return }


json.NewEncoder(w).Encode(user) json.NewEncoder(w).Encode(user)
} }


Loading…
Cancel
Save