fix : Login page, Register Page apply vuex
This commit is contained in:
@@ -4,5 +4,4 @@ import router from "./router";
|
||||
import store from "./store";
|
||||
|
||||
const app = createApp(App);
|
||||
app.use(router).use(store);
|
||||
app.mount('#app');
|
||||
app.use(router).use(store).mount('#app');
|
||||
@@ -1,9 +1,6 @@
|
||||
import axios from "axios";
|
||||
import { createStore } from 'vuex';
|
||||
|
||||
|
||||
const url = import.meta.env.VITE_BASE_URL;
|
||||
|
||||
export default createStore({
|
||||
state: {
|
||||
token: localStorage.getItem("token") || '',
|
||||
@@ -18,12 +15,12 @@ export default createStore({
|
||||
},
|
||||
},
|
||||
actions: {
|
||||
REGISTER({commit},{user}){
|
||||
return axios.post(url+'/api/users',{
|
||||
user
|
||||
}).then(response =>{
|
||||
console.log(response)
|
||||
})
|
||||
}
|
||||
},
|
||||
})
|
||||
LOGIN({commit}, user){
|
||||
commit("setUsername", user.username);
|
||||
commit("setToken", user.token);
|
||||
localStorage.setItem("username", user.username);
|
||||
localStorage.setItem("token", user.token);
|
||||
}
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
@@ -36,6 +36,7 @@
|
||||
import {reactive, ref} from "vue";
|
||||
import axios from "axios";
|
||||
import router from "@/router";
|
||||
import { useStore } from "vuex";
|
||||
|
||||
export default {
|
||||
name: "TheLogin",
|
||||
@@ -46,6 +47,8 @@ export default {
|
||||
password: "",
|
||||
})
|
||||
|
||||
const store = useStore();
|
||||
|
||||
let loginValidation = ref(false);
|
||||
|
||||
const signin = () => {
|
||||
@@ -54,11 +57,10 @@ export default {
|
||||
user
|
||||
})
|
||||
.then(response => {
|
||||
window.localStorage.setItem("token",response.data.user.token);
|
||||
store.dispatch("LOGIN",response.data.user);
|
||||
router.push("/");
|
||||
})
|
||||
.catch(error =>{
|
||||
console.log(error);
|
||||
const code = error.response.data.errors.code;
|
||||
if(code == "EMAIL_NULL_OR_INVALID"){
|
||||
loginValidation.value = true;
|
||||
|
||||
@@ -79,29 +79,25 @@ export default {
|
||||
};
|
||||
|
||||
const signup = () => {
|
||||
console.log(store.state)
|
||||
store.commit('REGISTER',user)
|
||||
|
||||
|
||||
|
||||
// axios.post(url+'/api/users',{
|
||||
// user
|
||||
// })
|
||||
// .then(response => {
|
||||
// window.localStorage.setItem("token",response.data.user.token);
|
||||
// allHideError();
|
||||
// router.push("/");
|
||||
// })
|
||||
// .catch(error =>{
|
||||
// const code = error.response.data.errors.code;
|
||||
// if(code == "DUPLICATE_EMAIL_USERNAME"){
|
||||
// showEmailUsernameError();
|
||||
// }else if(code == "DUPLICATE_EMAIL"){
|
||||
// showEmailError();
|
||||
// }else if(code == "DUPLICATE_USERNAME"){
|
||||
// showUsernameError();
|
||||
// }
|
||||
// })
|
||||
const url = import.meta.env.VITE_BASE_URL;
|
||||
axios.post(url+'/api/users',{
|
||||
user
|
||||
})
|
||||
.then(response => {
|
||||
store.dispatch("LOGIN",response.data.user);
|
||||
allHideError();
|
||||
router.push("/");
|
||||
})
|
||||
.catch(error =>{
|
||||
const code = error.response.data.errors.code;
|
||||
if(code == "DUPLICATE_EMAIL_USERNAME"){
|
||||
showEmailUsernameError();
|
||||
}else if(code == "DUPLICATE_EMAIL"){
|
||||
showEmailError();
|
||||
}else if(code == "DUPLICATE_USERNAME"){
|
||||
showUsernameError();
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
return { user, emailDuplicate, usernameDuplicate, signup, showEmailUsernameError,showEmailError, showUsernameError, allHideError }
|
||||
|
||||
@@ -40,7 +40,8 @@
|
||||
<script lang="ts">
|
||||
import axios from "axios";
|
||||
import router from "@/router";
|
||||
import {onMounted, reactive} from "vue";
|
||||
import { useStore } from "vuex";
|
||||
import { onMounted, reactive } from "vue";
|
||||
|
||||
export default {
|
||||
name: "TheSetting",
|
||||
@@ -55,6 +56,7 @@ export default {
|
||||
password: "",
|
||||
})
|
||||
const password = "";
|
||||
const store = useStore();
|
||||
|
||||
const getUser = (getuser: { bio: string; email: string; username: string; image: string; }) => {
|
||||
user.bio = getuser.bio
|
||||
@@ -64,14 +66,13 @@ export default {
|
||||
}
|
||||
|
||||
const updateUser = () => {
|
||||
console.log(token,user);
|
||||
axios.put(url+'/api/user',{user},{
|
||||
headers:{
|
||||
Authorization : "TOKEN " + token,
|
||||
"Content-Type": `application/json`,
|
||||
}})
|
||||
.then(response => {
|
||||
console.log(response);
|
||||
store.dispatch("LOGIN",response.data.user);
|
||||
router.push('/@'+ response.data.user.username);
|
||||
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user