Improve login/logout

Extract mutation types to mutation-types.js
This commit is contained in:
Jan Bader 2022-01-25 20:30:47 +00:00
parent 6dcf7da861
commit 74c4c7cb02
5 changed files with 35 additions and 15 deletions

View File

@ -93,9 +93,10 @@ func (h *Handler) loginPost(c *gin.Context) {
go h.Service.UpdateLastLogin(context.Background(), user.ID)
c.JSON(http.StatusOK, map[string]string{
"token": t,
})
c.JSON(http.StatusOK, struct {
Token string
User postgres.User
}{t, user})
}
type registerInformation struct {
@ -130,8 +131,20 @@ func (h *Handler) registerPost(c *gin.Context) {
Password: hash,
Email: register.Email,
}
_, err = h.Service.CreateUser(c.Request.Context(), createUser)
user, err := h.Service.CreateUser(c.Request.Context(), createUser)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
}
t, err := h.TokenVerifier.CreateToken(&user)
if err != nil {
c.AbortWithError(http.StatusUnauthorized, err)
}
go h.Service.UpdateLastLogin(context.Background(), user.ID)
c.JSON(http.StatusOK, struct {
Token string
User postgres.User
}{t, user})
}

View File

@ -1,5 +1,8 @@
<script>
// This starter template is using Vue 3 <script setup> SFCs
import { LOGOUT } from "./store/mutation-types";
// Check out https://v3.vuejs.org/api/sfc-script-setup.html#sfc-script-setup
export default {
data() {
@ -14,7 +17,8 @@ export default {
},
methods: {
logout () {
this.$store.dispatch("logout");
this.$store.commit(LOGOUT);
this.$router.push("/login")
},
toggleMenu () {
this.showMenu = !this.showMenu;

View File

@ -1,4 +1,6 @@
<script>
import { LOGIN_SUCCESS } from "../store/mutation-types";
export default {
computed: {
dashboard () {
@ -25,7 +27,8 @@ export default {
.then(x => x.json())
.then(x => {
this.$data.error = ""
this.$store.commit("setToken", x.token);
this.$store.commit(LOGIN_SUCCESS, x);
this.$router.replace("/dashboard");
})
.catch(x => this.$data.error = ["The entered credentials are invalid!"]);

View File

@ -58,7 +58,8 @@ $(document).ready(function () {
</form>
{{end}}-->
<script>
<script>import { LOGIN_SUCCESS } from "../store/mutation-types";
export default {
data() {
return {
@ -76,7 +77,7 @@ export default {
.then(x => x.json())
.then(x => {
this.$data.error = ""
this.$store.commit("setToken", x.token);
this.$store.commit(LOGIN_SUCCESS, x.token);
})
.catch(x => this.$data.error = ["Something went wrong!"]);

View File

@ -1,6 +1,7 @@
import { createStore } from 'vuex'
import dashboard from './dashboard/index'
import budget from './budget/index'
import { LOGIN_SUCCESS, LOGOUT } from './mutation-types'
const store = createStore({
state () {
@ -23,13 +24,11 @@ const store = createStore({
setTitle (state, title) {
document.title = "Budgeteer - " + title;
},
setToken(state, token) {
state.Session.Token = token;
}
},
actions: {
logout({state, commit, rootState}){
commit("setToken", null);
[LOGIN_SUCCESS](state, session) {
state.Session = session;
},
[LOGOUT](state, token) {
state.Session = { Token: null, User: null }
}
},
modules: {