Skip to content

Commit

Permalink
Merge pull request #9 from Omarabdul3ziz/3bot
Browse files Browse the repository at this point in the history
Adding OAuth with 3bot connect
  • Loading branch information
Omarabdul3ziz authored Sep 19, 2021
2 parents 023cd38 + f8a14e3 commit b1f2b87
Show file tree
Hide file tree
Showing 19 changed files with 543 additions and 217 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
.vscode/
node_modules/
.env
*.env
*.env
repo_*
5 changes: 5 additions & 0 deletions services/client/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@
>GitHub</router-link
>
</li>
<li v-if="!loggedIn">
<router-link class="navbar-link" id="right" to="/tribot"
>3Bot</router-link
>
</li>
<li v-if="loggedIn">
<router-link class="navbar-link" id="right" to="/logout"
>Logout</router-link
Expand Down
189 changes: 0 additions & 189 deletions services/client/src/components/Todo.vue

This file was deleted.

10 changes: 10 additions & 0 deletions services/client/src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,13 @@ new Vue({
store,
render: (h) => h(App),
}).$mount("#app");

// Does it help?
const allowCrossDomain = function (req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Methods", "*");
res.header("Access-Control-Allow-Headers", "*");
next();
};

Vue.use(allowCrossDomain);
15 changes: 12 additions & 3 deletions services/client/src/router/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import Todo from "@/views/Todo.vue";
import Login from "@/views/Login.vue";
import Register from "@/views/Register.vue";
import Github from "@/views/Github.vue";
import Tribot from "@/views/Tribot.vue";
import Logout from "@/views/Logout.vue";

Vue.use(VueRouter);
Expand All @@ -21,9 +22,9 @@ const routes = [
path: "/todo",
name: "Todo",
component: Todo,
// meta: {
// requiresAuth: true,
// },
meta: {
requiresAuth: true,
},
},
{
path: "/login",
Expand All @@ -49,6 +50,14 @@ const routes = [
requiresVisitor: true,
},
},
{
path: "/tribot",
name: "Tribot",
component: Tribot,
meta: {
requiresVisitor: true,
},
},
{
path: "/logout",
name: "Logout",
Expand Down
2 changes: 1 addition & 1 deletion services/client/src/store/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export default new Vuex.Store({
state: {
token: localStorage.getItem("access_token") || null,
// access_token_cookie: Vue.$cookies.get("access_token_cookie") || null,
api_url: "http://localhost:8080",
api_url: "http://127.0.0.1:8080",
},
mutations: {
updateToken(state, token) {
Expand Down
14 changes: 14 additions & 0 deletions services/client/src/views/Home.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,21 @@
</template>

<script>
import Vue from "vue";
import VueCookie from "vue-cookie";
Vue.use(VueCookie);
export default {
name: "Home",
beforeCreate() {
var token = this.$cookie.get("access_token_cookie");
console.log(token);
// add to local storage
localStorage.setItem("access_token", token); // token stores in cookies
// update the store state token
this.$store.commit("updateToken", token);
},
};
</script>
22 changes: 19 additions & 3 deletions services/client/src/views/Logout.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
import Vue from "vue";
import axios from "axios";
import VueAxios from "vue-axios";
import VueCookie from "vue-cookie";
Vue.use(VueCookie);
Vue.use(VueAxios, axios);
export default {
name: "Logout",
Expand All @@ -16,16 +18,30 @@ export default {
};
},
created() {
this.distroyToken();
this.logout();
},
methods: {
logout() {
// remove token from cookie
this.$cookie.delete("access_token_cookie");
// remove token from localStorage
localStorage.clear(); // not the best practice but workes for now
// remove the token from store state
this.$store.commit("distroyToken");
// redirect
this.$router.push({ name: "Login" });
},
distroyToken() {
const path = this.baseUrl + "/auth/logout";
axios.defaults.headers.common["Authorization"] =
Vue.axios.defaults.headers.common["Authorization"] =
"Bearer " + this.$store.state.token;
axios.get(path).then((response) => {
Vue.axios.get(path).then((response) => {
// remove the cookie from server
console.log(response.data.message);
Expand Down
13 changes: 6 additions & 7 deletions services/client/src/views/Todo.vue
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,13 @@ Vue.use(VueAxios, axios);
export default {
name: "Todo",
beforeCreate() {
var token = this.$cookie.get("access_token_cookie");
console.log(token);
// add to local storage
localStorage.setItem("access_token", token); // token stores in cookies
var token = this.$cookie.get("access_token_cookie");
console.log(token);
// add to local storage
localStorage.setItem("access_token", token); // token stores in cookies
// update the store state token
this.$store.commit("updateToken", token);
// update the store state token
this.$store.commit("updateToken", token);
},
data() {
Expand All @@ -86,7 +86,6 @@ export default {
},
},
created() {
// this.loadToken();
this.getTodos();
Expand Down
Loading

0 comments on commit b1f2b87

Please sign in to comment.