Improve login/logout
Extract mutation types to mutation-types.js
This commit is contained in:
parent
6dcf7da861
commit
74c4c7cb02
@ -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})
|
||||
}
|
||||
|
@ -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;
|
||||
|
@ -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!"]);
|
||||
|
||||
|
@ -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!"]);
|
||||
|
||||
|
@ -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;
|
||||
}
|
||||
[LOGIN_SUCCESS](state, session) {
|
||||
state.Session = session;
|
||||
},
|
||||
actions: {
|
||||
logout({state, commit, rootState}){
|
||||
commit("setToken", null);
|
||||
[LOGOUT](state, token) {
|
||||
state.Session = { Token: null, User: null }
|
||||
}
|
||||
},
|
||||
modules: {
|
||||
|
Loading…
x
Reference in New Issue
Block a user