Commit fe0894bb authored by Reko Meriö's avatar Reko Meriö

merge

parents 98e2aaa8 d7a7bd84
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;
class AuthController extends Controller
{
/**
* Create a new AuthController instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('auth:api', ['except' => ['login']]);
}
/**
* Get a JWT via given credentials.
*
* @return \Illuminate\Http\JsonResponse
*/
public function login()
{
$credentials = request(['email', 'password']);
if (! $token = auth('api')->attempt($credentials)) {
return response()->json(['error' => 'Unauthorized'], 401);
}
return $this->respondWithToken($token);
}
/**
* Get the authenticated User.
*
* @return \Illuminate\Http\JsonResponse
*/
public function me()
{
return response()->json(auth('api')->user());
}
/**
* Log the user out (Invalidate the token).
*
* @return \Illuminate\Http\JsonResponse
*/
public function logout()
{
auth()->logout();
return response()->json(['message' => 'Successfully logged out']);
}
/**
* Refresh a token.
*
* @return \Illuminate\Http\JsonResponse
*/
public function refresh()
{
return $this->respondWithToken(auth('api')->refresh());
}
/**
* Get the token array structure.
*
* @param string $token
*
* @return \Illuminate\Http\JsonResponse
*/
protected function respondWithToken($token)
{
return response()->json([
'access_token' => $token,
'user' => $this->guard()->user(),
'token_type' => 'bearer',
'expires_in' => auth('api')->factory()->getTTL() * 60
]);
}
public function guard() {
return Auth::Guard('api');
}
}
......@@ -2,11 +2,12 @@
namespace App;
use Tymon\JWTAuth\Contracts\JWTSubject;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
class User extends Authenticatable implements JWTSubject
{
use Notifiable;
......@@ -36,4 +37,23 @@ class User extends Authenticatable
protected $casts = [
'email_verified_at' => 'datetime',
];
/**
* Get the identifier that will be stored in the subject claim of the JWT.
*
* @return mixed
*/
public function getJWTIdentifier()
{
return $this->getKey();
}
/**
* Return a key value array, containing any custom claims to be added to the JWT.
*
* @return array
*/
public function getJWTCustomClaims()
{
return [];
}
}
{
"name": "laravel/laravel",
"type": "project",
......@@ -12,7 +13,8 @@
"dmitry-ivanov/dark-sky-api": "^1.0",
"fideloper/proxy": "^4.0",
"laravel/framework": "5.8.*",
"laravel/tinker": "^1.0"
"laravel/tinker": "^1.0",
"tymon/jwt-auth": "^1.0.0"
},
"require-dev": {
"beyondcode/laravel-dump-server": "^1.0",
......@@ -61,3 +63,4 @@
]
}
}
This diff is collapsed.
......@@ -42,9 +42,9 @@ return [
],
'api' => [
'driver' => 'token',
'driver' => 'jwt',
'provider' => 'users',
'hash' => false,
/* 'hash' => false,*/
],
],
......
......@@ -8800,6 +8800,11 @@
"integrity": "sha512-1wFuMUIM16MDJRCrpbpuEPTUGmM5QMUg0cr3KFwra2XgOgFcPGDQHDh3CszSCD2Zewc/dh/pamNEW8CbfDebUw==",
"dev": true
},
"validate.js": {
"version": "0.12.0",
"resolved": "https://registry.npmjs.org/validate.js/-/validate.js-0.12.0.tgz",
"integrity": "sha512-/x2RJSvbqEyxKj0RPN4xaRquK+EggjeVXiDDEyrJzsJogjtiZ9ov7lj/svVb4DM5Q5braQF4cooAryQbUwOxlA=="
},
"vary": {
"version": "1.1.2",
"resolved": "https://registry.npmjs.org/vary/-/vary-1.1.2.tgz",
......@@ -8903,6 +8908,11 @@
"resolved": "https://registry.npmjs.org/vuetify/-/vuetify-1.5.7.tgz",
"integrity": "sha512-e7Vvj9gh41Pth7pXJxH1hrm8wfnvWt3nvxPCnwBWTgr4kiUXTG4CaUBqWVVgyfLjJGJSyR1y1EoRNEP5tJv0HQ=="
},
"vuex": {
"version": "3.1.0",
"resolved": "https://registry.npmjs.org/vuex/-/vuex-3.1.0.tgz",
"integrity": "sha512-mdHeHT/7u4BncpUZMlxNaIdcN/HIt1GsGG5LKByArvYG/v6DvHcOxvDCts+7SRdCoIRGllK8IMZvQtQXLppDYg=="
},
"watchpack": {
"version": "1.6.0",
"resolved": "https://registry.npmjs.org/watchpack/-/watchpack-1.6.0.tgz",
......
......@@ -24,10 +24,12 @@
"vue-template-compiler": "^2.6.10"
},
"dependencies": {
"validate.js": "^0.12.0",
"vue-axios": "^2.1.4",
"vue-material": "^1.0.0-beta-10.2",
"vue-pose": "^0.5.0",
"vue-router": "^3.0.2",
"vuetify": "^1.5.7"
"vuetify": "^1.5.7",
"vuex": "^3.1.0"
}
}
This diff is collapsed.
......@@ -11,18 +11,22 @@ window.Vue = require('vue');
import Vue from 'vue'
import VueRouter from 'vue-router'
import axios from 'axios';
import Vuex from 'vuex';
import Vuetify from 'vuetify'
import 'vuetify/dist/vuetify.min.css' // Ensure you are using css-loader
import VueMaterial from 'vue-material'
import StoreData from './store'
Vue.use(VueMaterial)
Vue.use(Vuetify)
Vue.use(VueRouter)
Vue.use(VueMaterial);
Vue.use(Vuex);
Vue.use(Vuetify);
Vue.use(VueRouter);
import App from './components/App'
import Main from './components/Main'
import Hello from './components/Hello'
import Home from './components/Home'
import Login from './components/auth/Login'
Vue.component('example-component', require('./components/ExampleComponent.vue').default);
......@@ -34,7 +38,7 @@ Vue.component('sidebar', require('./components/Sidebar.vue').default);
Vue.component('favorites', require('./components/Favorites.vue').default);
const store = new Vuex.Store(StoreData);
const router = new VueRouter({
mode: 'history',
......@@ -42,7 +46,10 @@ const router = new VueRouter({
{
path: '/',
name: 'home',
component: Main
component: Main,
meta: {
requiresAuth: true
}
},
{
path: '/hello',
......@@ -50,13 +57,34 @@ const router = new VueRouter({
component: Hello
},
{
path: '/login',
component: Login
},
],
});
router.beforeEach((to, from, next) => {
const requiresAuth = to.matched.some(record => record.meta.requiresAuth);
const currentUser = store.state.currentUser;
if(requiresAuth && !currentUser) {
next('login');
}
else if(to.path == '/login' && currentUser) {
next('/');
}
else {
next();
}
});
const app = new Vue({
el: '#app',
components: {
App
},
router,
store,
});
<template>
<v-app id="inspire">
<v-app id="app">
<v-navigation-drawer v-model="drawer" fixed app>
<v-list dense>
<v-list-tile @click="">
......@@ -22,7 +22,36 @@
</v-navigation-drawer>
<v-toolbar color="light-green accent-4" dark fixed app>
<v-toolbar-side-icon @click.stop="drawer = !drawer"></v-toolbar-side-icon>
<v-toolbar-title>Wear Right</v-toolbar-title>
<v-toolbar-title> {{ welcome }}</v-toolbar-title>
<v-spacer></v-spacer>
<!--<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav ml-auto">-->
<template v-if="!currentUser">
<v-btn @click.prevent="logout" to="/login" flat color="white">
<span>Kirjaudu sisään</span>
<v-icon right>exit_to_app</v-icon>
</v-btn>
<!-- <li>
<router-link to="/login" class="nav-link">Kirjaudu sisään</router-link>
</li>-->
</template>
<template v-else>
<!-- <li class="nav-link dropdown">
<a id="navbarDropdown" class="nav-item dropdown-toggle" href="#" role="button" data-toggle="dropdown" aria-expanded="false" aria-haspopup="true">
{{ currentUser.name }} <span class="caret"></span>
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdown">
<a href="#!" @click.prevent="logout" class="dropdown-item">Kirjaudu ulos</a>
</div>
</li>-->
<v-btn @click.prevent="logout" flat color="white">
<span>Kirjaudu ulos</span>
<v-icon right>exit_to_app</v-icon>
</v-btn>
</template>
<!-- </ul>
</div> -->
</v-toolbar>
<v-content>
<v-container fluid fill-height>
......@@ -36,7 +65,7 @@
</v-container>
</v-content>
<v-footer color="light-green accent-4" app>
<span class="white--text">&copy; 2019</span>
<span class="white--text">&copy; Lumiukot 2019</span>
</v-footer>
</v-app>
</template>
......@@ -49,7 +78,21 @@
}),
props: {
source: String
}
},
computed: {
welcome(){
return this.$store.getters.welcome
},
currentUser() {
return this.$store.getters.currentUser
}
},
methods: {
logout() {
this.$store.commit('logout');
this.$router.push('/login');
}
}
}
</script>
......@@ -13,7 +13,7 @@
if (this.value) {
this.welcome = this.value;
}
}
},
}
</script>
<template>
<v-layout row wrap>
<v-flex xs12 offset-md4 md4>
<!--<div class="login row justify-content-center">
<div class="col-md-4">
<div class="card">
<div class="card-header">login</div>
<div class="card-body">
<form @submit.prevent="authenticate">
<div class="form-group row">
<label for="email">Sähköposti:</label>
<input type="email" v-model="form.email" class="form-control" placeholder="Sähköposti">
</div>
<div class="form-group row">
<label for="password">Salasana:</label>
<input type="password" v-model="form.password" class="form-control" placeholder="Salasana">
</div>
<div class="form-group row">
<input type="submit" value="Kirjaudu sisään" />
</div>
<div class="form-group row" v-if="authError">
<p class="auth-error"> {{ authError }} </p>
</div>
</form>
</div>
</div>-->
<v-card flat color="transparent">
<v-card-title primary-title>
<h4>Wear Right</h4>
</v-card-title>
<v-form @submit.prevent="authenticate">
<v-text-field v-model="form.email" prepend-icon="person" name="email" label="Sähköposti"></v-text-field>
<v-text-field v-model="form.password" prepend-icon="lock" name="password" label="Salasana" type="password"></v-text-field>
<v-card-actions>
<v-btn @click="authenticate" primary large block>Kirjaudu sisään</v-btn>
</v-card-actions>
</v-form>
</v-card>
</v-flex>
</v-layout>
</div>
</div>
</template>
<script>
import {login} from '../../helpers/auth';
export default {
name: "login",
data() {
return {
form: {
email: '',
password: ''
},
error: null
};
},
methods: {
authenticate() {
this.$store.dispatch('login');
login(this.$data.form)
.then((res) => {
this.$store.commit("loginSuccess", res);
this.$router.push({path: '/'});
})
.catch((error) => {
this.$store.commit("loginFailed", {error});
});
}
},
computed: {
authError() {
return this.$store.getters.authError
}
}
}
</script>
<style scoped>
</styler>
\ No newline at end of file
export function login(credentials) {
return new Promise((res, rej) => {
axios.post('/api/auth/login', credentials)
.then((response) => {
res(response.data);
})
.catch((err) => {
rej("Virheellinen sähköpostiosoite tai salasana");
})
})
}
export function getLocalUser() {
const userStr = localStorage.getItem("user");
if(!userStr) {
return null;
}
return JSON.parse(userStr);
}
\ No newline at end of file
import { getLocalUser } from "./helpers/auth";
const user = getLocalUser();
export default {
state: {
currentUser: user,
isLoggedIn: !!user,
loading: false,
auth_error: null,
customers: [],
welcomeMessage: 'Wear Right'
},
getters: {
welcome(state) {
return state.welcomeMessage;
},
isLoading(state) {
return state.loading;
},
isLoggedIn(state) {
return state.isLoggedIn;
},
currentUser(state) {
return state.currentUser;
},
authError(state) {
return state.auth_error;
},
customers(state) {
return state.customers;
}
},
mutations: {
login(state) {
state.loading = true;
state.auth_error = null;
},
loginSuccess(state, payload) {
state.auth_error = null;
state.isLoggedIn = true;
state.loading = false;
state.currentUser = Object.assign({}, payload.user, {token: payload.access_token});
localStorage.setItem("user", JSON.stringify(state.currentUser));
},
loginFailed(state, payload) {
state.loading = false;
state.auth_error = payload.error;
},
logout(state) {
localStorage.removeItem("user");
state.isLoggedIn = false;
state.currentUser = null;
}
},
actions: {
login(context) {
context.commit("login");
}
}
};
......@@ -7,7 +7,7 @@
<meta name="csrf-token" content="{{ csrf_token() }}">
<link href="https://fonts.googleapis.com/css?family=Nunito:200,600" rel="stylesheet">
<link rel="stylesheet" href="//fonts.googleapis.com/css?family=Roboto:400,500,700,400italic|Material+Icons">
<title>Vue SPA Demo</title>
<title>Wear Right</title>
</head>
<body>
<div id="app">
......
......@@ -13,9 +13,9 @@ use Illuminate\Http\Request;
|
*/
Route::middleware('auth:api')->get('/user', function (Request $request) {
/*Route::middleware('auth:api')->get('/user', function (Request $request) {
return $request->user();
});
});*/
......@@ -29,3 +29,18 @@ Route::get('/trol2','DBController@populatevaatetable');
Route::get('/trol3','DBController@populatevaatetable2');
Route::get('/trol4','DBController@populatetablet');
Route::get('/saa','WeatherController@push');
Route::group([
'middleware' => 'api',
'prefix' => 'auth'
], function ($router) {
Route::post('login', 'AuthController@login');
Route::post('logout', 'AuthController@logout');
Route::post('refresh', 'AuthController@refresh');
Route::post('me', 'AuthController@me');
});
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