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) go h.Service.UpdateLastLogin(context.Background(), user.ID)
c.JSON(http.StatusOK, map[string]string{ c.JSON(http.StatusOK, struct {
"token": t, Token string
}) User postgres.User
}{t, user})
} }
type registerInformation struct { type registerInformation struct {
@ -130,8 +131,20 @@ func (h *Handler) registerPost(c *gin.Context) {
Password: hash, Password: hash,
Email: register.Email, Email: register.Email,
} }
_, err = h.Service.CreateUser(c.Request.Context(), createUser) user, err := h.Service.CreateUser(c.Request.Context(), createUser)
if err != nil { if err != nil {
c.AbortWithError(http.StatusInternalServerError, err) 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> <script>
// This starter template is using Vue 3 <script setup> SFCs // 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 // Check out https://v3.vuejs.org/api/sfc-script-setup.html#sfc-script-setup
export default { export default {
data() { data() {
@ -14,7 +17,8 @@ export default {
}, },
methods: { methods: {
logout () { logout () {
this.$store.dispatch("logout"); this.$store.commit(LOGOUT);
this.$router.push("/login")
}, },
toggleMenu () { toggleMenu () {
this.showMenu = !this.showMenu; this.showMenu = !this.showMenu;

View File

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

View File

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

View File

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