Commit 26f0e320 authored by Khalid Ali's avatar Khalid Ali
Browse files

POST & GET request works, service separated from backend

parent 100a3ae9
Pipeline #4766 passed with stages
in 4 minutes and 22 seconds
......@@ -4434,7 +4434,7 @@
},
"get-stream": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/get-stream/-/get-stream-3.0.0.tgz",
"resolved": "http://registry.npmjs.org/get-stream/-/get-stream-3.0.0.tgz",
"integrity": "sha1-jpQ9E1jcN1VQVOy+LtsFqhdO3hQ="
},
"lru-cache": {
......
......@@ -2,10 +2,12 @@ import axios from 'axios';
const axiosInstance = axios.create({
// baseURL: `/api/`,
// baseURL: `https://localhost:9090/bs/api`,
baseURL: `https://localhost:9090/`,
headers: {
// 'X-CSRF-TOKEN': selector.getAttribute('content')
// 'Access-Control-Allow-Origin': '*'
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': '*',
'Access-Control-Allow-Headers': '*',
},
withCredentials: true,
maxRedirects: 10,
......
......@@ -16,7 +16,7 @@
<div class="navbar-collapse collapse" id="navbar">
<ul class="navbar-nav mr-auto">
<li class="nav-item"><router-link to="/create_listing" class="nav-link">Create Listing</router-link></li>
<li class="nav-item"><router-link to="/all_listing" class="nav-link">Latest Listings</router-link></li>
<li class="nav-item"><router-link to="/latest_listing" class="nav-link">Latest Listings</router-link></li>
<li class="nav-item"><router-link to="/about" class="nav-link">About</router-link></li>
</ul>
</div>
......
......@@ -35,5 +35,13 @@ export default new Router({
title: 'SRCT Bookshare - Create Listing',
},
},
{
path: '/latest_listing',
name: 'latest_listing',
component: () => import ('./views/LatestListing.vue'),
meta: {
title: 'SRCT Bookshare - Latest Listing',
},
},
],
});
......@@ -159,7 +159,7 @@ export default class CreateListing extends Vue {
private created() {
// Must be logged in
API.get('/bs/api/').catch((error) => {
API.get('/bs/api/listing').catch((error) => {
// Redirect to backend login
if (error.response === undefined || error.response.status === 403) {
location.replace('https://localhost:9090/bs/api/login');
......@@ -167,13 +167,18 @@ export default class CreateListing extends Vue {
});
// Get CSRF Token
API.get('/').then((response) => {
API.get('/bs/api/').then((response) => {
console.log(response);
const token = response.headers['x-csrf-token'];
if (response.headers['x-csrf-token'] != null) {
localStorage.setItem('csrf_token', token);
}
}).catch((error) => {
// console.log(error.response);
const token = error.headers['x-csrf-token'];
if (error.headers['x-csrf-token'] != null) {
localStorage.setItem('csrf_token', token);
}
});
}
......@@ -199,8 +204,9 @@ export default class CreateListing extends Vue {
.catch(function(this: CreateListing, error) {
// Redirect to backend login
if (error.response === undefined || error.response.status === 403) {
location.replace('https://localhost:9090/bs/api/login');
//location.replace('https://localhost:9090/bs/api/login');
}
console.log(error);
});
}
......
......@@ -36,7 +36,7 @@
<router-link to="/create_listing" class="btn btn-primary btn-lg btn-block">I <em>have</em> a book</router-link>
</div>
<div class="col-sm-5">
<router-link to="/all_listing" class="btn btn-primary btn-lg btn-block">I <em>need</em> a book</router-link>
<router-link to="/latest_listing" class="btn btn-primary btn-lg btn-block">I <em>need</em> a book</router-link>
</div>
</div>
</div>
......
<template>
<div id="listings">
{{ info }}
</div>
</template>
<script lang="ts">
import API from '../api';
import { Component, Vue } from 'vue-property-decorator';
@Component
export default class LatestListing extends Vue {
private info:any = "";
private created() {
// Must be logged in
API.get('bs/api/listing/')
.then(response => (this.info = response))
.catch((error) => {
// Redirect to backend login
console.log(error);
if (error.response === undefined || error.response.status === 403) {
location.replace('https://localhost:9090/bs/api/login');
console.log(error);
}
if (error.response.status === 404) {
API.get('bs/api/listing/');
}
});
}
}
</script>
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment