Skip to content

Commit

Permalink
unnecessary vuex stores remove, code structure improvement
Browse files Browse the repository at this point in the history
using tab instead of space, tab size 2, unnecessary vuex store mudule removed, used noty as notification plugin, better code structure
  • Loading branch information
anindya-dhruba committed Jan 7, 2018
1 parent d477940 commit 5f6a337
Show file tree
Hide file tree
Showing 33 changed files with 758 additions and 962 deletions.
44 changes: 23 additions & 21 deletions app/Http/Controllers/AuthenticateController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,27 @@

class AuthenticateController extends Controller
{
public function authenticate(Request $request)
{
$rules = [
'email' => 'required|email',
'password' => 'required'
];

$this->validate($request, $rules);

$credentials = $request->only('email', 'password');

try {
if (! $token = JWTAuth::attempt($credentials)) {
return response()->json(['error' => 'Invalid Login Credential'], 401);
}
} catch (JWTException $e) {
return response()->json(['error' => 'Could not create token'], 500);
}

return response()->json(compact('token'));
}
public function authenticate(Request $request)
{
$rules = [
'email' => 'required|email',
'password' => 'required'
];

$this->validate($request, $rules);

$credentials = $request->only('email', 'password');

try {
if(!$token = JWTAuth::attempt($credentials)) {
return response()->json(['error' => 'Invalid Login Credential'], 401);
}
} catch(JWTException $e) {
return response()->json(['error' => 'Could not create token'], 500);
}

$user = $request->user();

return response()->json(compact('token', 'user'));
}
}
74 changes: 37 additions & 37 deletions app/Http/Controllers/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,41 +6,41 @@

class UserController extends Controller
{
public function show(Request $request)
{
return $request->user();
}
public function updateProfile(Request $request)
{
$rules = [
'name' => 'required',
'email' => 'required|email|',
];

$this->validate($request, $rules);
$user = $request->user();
$user->name = $request->input('name');
$user->email = $request->input('email');
$user->save();

return response()->json(compact('user'));
}

public function updatePassword(Request $request)
{
$rules = [
'new_password' => 'required',
'confirm_new_password' => 'required|same:new_password'
];

$this->validate($request, $rules);

$user = $request->user();
$user->password = bcrypt($request->input('new_password'));
$user->saveOrFail();

return response()->json(compact('user'));
}
public function show(Request $request)
{
return $request->user();
}

public function updateProfile(Request $request)
{
$rules = [
'name' => 'required',
'email' => 'required|email|',
];

$this->validate($request, $rules);

$user = $request->user();
$user->name = $request->input('name');
$user->email = $request->input('email');
$user->save();

return response()->json(compact('user'));
}

public function updatePassword(Request $request)
{
$rules = [
'new_password' => 'required',
'confirm_new_password' => 'required|same:new_password'
];

$this->validate($request, $rules);

$user = $request->user();
$user->password = bcrypt($request->input('new_password'));
$user->saveOrFail();

return response()->json(compact('user'));
}
}
15 changes: 15 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
"cross-env": "^5.1",
"jquery": "^3.2",
"laravel-mix": "^1.0",
"lodash": "^4.17.4",
"vue": "^2.5.7",
"vue-router": "^3.0.1",
"vuejs-noty": "^0.1.2",
"vuex": "^3.0.1"
},
"dependencies": {}
Expand Down
4 changes: 2 additions & 2 deletions public/css/app.css

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion public/js/app.js

Large diffs are not rendered by default.

49 changes: 29 additions & 20 deletions resources/assets/js/app.js
Original file line number Diff line number Diff line change
@@ -1,49 +1,58 @@
import $ from 'jquery';
import _ from 'lodash';
import Vue from 'vue';
import VueNoty from 'vuejs-noty';
import axios from 'axios';

window.$ = window.jQuery = $;
window.axios = axios;
require('bootstrap-sass');

Vue.use(VueNoty, {
progressBar: false,
layout: 'bottomRight',
theme: 'bootstrap-v3',
timeout: 5000
});

import router from './routes';
import router from './router';
import store from './store/index';
import App from './components/App.vue';
import jwtToken from './helpers/jwt-token';

axios.interceptors.request.use(config => {
config.headers['X-CSRF-TOKEN'] = window.Laravel.csrfToken;
config.headers['X-Requested-With'] = 'XMLHttpRequest';
config.headers['X-CSRF-TOKEN'] = window.Laravel.csrfToken;
config.headers['X-Requested-With'] = 'XMLHttpRequest';

if(jwtToken.getToken()) {
config.headers['Authorization'] = 'Bearer '+ jwtToken.getToken();
}
if (jwtToken.getToken()) {
config.headers['Authorization'] = 'Bearer ' + jwtToken.getToken();
}

return config;
return config;
}, error => {
return Promise.reject(error);
return Promise.reject(error);
});

axios.interceptors.response.use(response => {
return response;
return response;
}, error => {
let errorResponseData = error.response.data;
let errorResponseData = error.response.data;

const errors = ["token_invalid", "token_expired", "token_not_provided"];

if(errorResponseData.error && (errorResponseData.error === "token_invalid" || errorResponseData.error === "token_expired" || errorResponseData.error === 'token_not_provided')) {
store.dispatch('logoutRequest')
.then(()=> {
router.push({name: 'login'});
});
}
if (errorResponseData.error && errors.includes(errorResponseData.error)) {
store.dispatch('unsetAuthUser')
.then(() => {
jwtToken.removeToken();
router.push({name: 'login'});
});
}

return Promise.reject(error);
return Promise.reject(error);
});

Vue.component('app', App);

const app = new Vue({
router,
store
router,
store
}).$mount('#app');
39 changes: 15 additions & 24 deletions resources/assets/js/components/App.vue
Original file line number Diff line number Diff line change
@@ -1,30 +1,21 @@
<template>
<div>
<top-menu></top-menu>
<notification></notification>
<transition name="fade" mode="out-in">
<router-view></router-view>
</transition>
<app-footer></app-footer>
</div>
<div>
<top-menu></top-menu>
<transition name="fade" mode="out-in">
<router-view></router-view>
</transition>
<app-footer></app-footer>
</div>
</template>

<script>
import TopMenu from './shared/TopMenu.vue'
import Notification from './shared/Notification.vue'
import AppFooter from './shared/AppFooter.vue'
import jwtToken from './../helpers/jwt-token';
import TopMenu from './shared/TopMenu.vue';
import AppFooter from './shared/AppFooter.vue';
export default {
created() {
if(jwtToken.getToken()) {
this.$store.dispatch('setAuthUser');
}
},
components: {
'top-menu': TopMenu,
'notification': Notification,
'app-footer': AppFooter
}
}
export default {
components: {
'top-menu': TopMenu,
'app-footer': AppFooter
}
}
</script>
28 changes: 14 additions & 14 deletions resources/assets/js/components/home/Home.vue
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
<template>
<div class="container">
<div class="jumbotron">
<h1 v-once>{{siteName}}</h1>
<p>Starter Boilerplate SPA made with Laravel 5.5, JWT Auth, Vue 2, Vue-router 3, Vuex 3, Axios and love</p>
</div>
</div>
<div class="container">
<div class="jumbotron">
<h1 v-once>{{siteName}}</h1>
<p>Starter Boilerplate SPA made with Laravel 5.5, JWT Auth, Vue 2, Vue-router 3, Vuex 3, Axios and love</p>
</div>
</div>
</template>

<script>
import {siteName} from './../../config';
import {siteName} from './../../config';
export default {
data() {
return {
siteName: siteName
}
}
}
export default {
data() {
return {
siteName: siteName
}
}
}
</script>
60 changes: 36 additions & 24 deletions resources/assets/js/components/login/Login.vue
Original file line number Diff line number Diff line change
@@ -1,30 +1,42 @@
<template>
<div class="container">
<div class="row">
<div class="col-md-8">
<div class="page-header">
<h1>Login</h1>
</div>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Aspernatur corporis deserunt, dolorem eligendi esse eum illo illum inventore libero minus nam numquam officiis praesentium quas quasi repudiandae sed tempore voluptatibus?</p>
</div>
<div class="col-md-4">
<div class="panel panel-default">
<div class="panel-heading">Login</div>
<div class="panel-body">
<login-form></login-form>
</div>
</div>
</div>
</div>
</div>
<div class="container">
<div class="row">
<div class="col-md-8">
<div class="page-header">
<h1>Login</h1>
</div>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Aspernatur corporis deserunt, dolorem eligendi esse eum illo illum inventore libero minus nam numquam officiis praesentium quas quasi repudiandae sed tempore voluptatibus?</p>
</div>
<div class="col-md-4">
<div class="panel panel-default">
<div class="panel-heading">Login</div>
<div class="panel-body">
<login-form @loginSuccess="loginSuccess"></login-form>
</div>
</div>
</div>
</div>
</div>
</template>

<script>
import LoginForm from './../login/LoginForm.vue'
import LoginForm from './../login/LoginForm.vue'
import jwtToken from "../../helpers/jwt-token";
import {mapActions} from "vuex";
export default {
components: {
'login-form': LoginForm
}
}
export default {
components: {
'login-form': LoginForm
},
methods: {
...mapActions([
'setAuthUser'
]),
loginSuccess(data) {
jwtToken.setToken(data.token);
this.setAuthUser(data.user);
this.$router.push({name: 'profile'});
}
}
}
</script>
Loading

0 comments on commit 5f6a337

Please sign in to comment.