From 254a84f8786de556dee13b5438d63f6fbb1379f5 Mon Sep 17 00:00:00 2001 From: Jad Joubran Date: Wed, 23 Mar 2016 00:46:34 +0200 Subject: [PATCH] Login functionality using satellizer #202 --- .../login-form/login-form.component.html | 28 ++++++++------- .../login-form/login-form.component.js | 36 ++++++++++++------- angular/app/pages/header/header.page.html | 2 +- angular/config/satellizer.config.js | 4 +-- app/Http/Controllers/Auth/AuthController.php | 10 ++++-- app/Http/routes.php | 2 -- config/database.php | 1 + .../2014_10_12_000000_create_users_table.php | 1 - 8 files changed, 50 insertions(+), 34 deletions(-) diff --git a/angular/app/components/login-form/login-form.component.html b/angular/app/components/login-form/login-form.component.html index 2b12e81..dfba7e4 100644 --- a/angular/app/components/login-form/login-form.component.html +++ b/angular/app/components/login-form/login-form.component.html @@ -1,15 +1,17 @@ -
- - - - -
+
+
+ + + + +
-
- - - - -
+
+ + + + +
-Login + Login +
diff --git a/angular/app/components/login-form/login-form.component.js b/angular/app/components/login-form/login-form.component.js index c07da46..f877486 100644 --- a/angular/app/components/login-form/login-form.component.js +++ b/angular/app/components/login-form/login-form.component.js @@ -1,17 +1,29 @@ -class LoginFormController{ - constructor(){ - 'ngInject'; +class LoginFormController { + constructor($auth) { + 'ngInject'; - this.email = ''; - this.password = ''; - } + this.$auth = $auth; + + this.email = ''; + this.password = ''; + } + + login() { + var user = { + email: this.email, + password: this.password + }; + + this.$auth.login(user) + .then((response) => { + this.$auth.setToken(response.data.data.token); + }); + } } export const LoginFormComponent = { - templateUrl: './views/app/components/login-form/login-form.component.html', - controller: LoginFormController, - controllerAs: 'vm', - bindings: {} + templateUrl: './views/app/components/login-form/login-form.component.html', + controller: LoginFormController, + controllerAs: 'vm', + bindings: {} } - - diff --git a/angular/app/pages/header/header.page.html b/angular/app/pages/header/header.page.html index bdff2b1..123a560 100644 --- a/angular/app/pages/header/header.page.html +++ b/angular/app/pages/header/header.page.html @@ -2,7 +2,7 @@
- +
Docs Screencasts diff --git a/angular/config/satellizer.config.js b/angular/config/satellizer.config.js index 2a95809..6c46c73 100644 --- a/angular/config/satellizer.config.js +++ b/angular/config/satellizer.config.js @@ -5,7 +5,7 @@ export function SatellizerConfig($authProvider) { return true; } - $authProvider.loginUrl = '/auth/login'; - $authProvider.signupUrl = '/auth/signup'; + $authProvider.loginUrl = '/api/auth/login'; + $authProvider.signupUrl = '/api/auth/register'; } diff --git a/app/Http/Controllers/Auth/AuthController.php b/app/Http/Controllers/Auth/AuthController.php index de94654..11bf481 100644 --- a/app/Http/Controllers/Auth/AuthController.php +++ b/app/Http/Controllers/Auth/AuthController.php @@ -2,8 +2,10 @@ namespace App\Http\Controllers\Auth; +use Auth; use JWTAuth; use App\User; +use Illuminate\Http\Request; use App\Http\Controllers\Controller; class AuthController extends Controller @@ -15,13 +17,15 @@ public function postLogin(Request $request) try { // verify the credentials and create a token for the user if (! $token = JWTAuth::attempt($credentials)) { - return response()->error('Invalid credentials', Response::HTTP_UNAUTHORIZED); + return response()->error('Invalid credentials', 401); } } catch (\JWTException $e) { - return response()->error('Could not create token', Response::HTTP_INTERNAL_SERVER_ERROR); + return response()->error('Could not create token', 500); } - return response()->success(compact('token')); + $user = Auth::user(); + + return response()->success(compact('user', 'token')); } public function postRegister() diff --git a/app/Http/routes.php b/app/Http/routes.php index 4317285..ce69967 100644 --- a/app/Http/routes.php +++ b/app/Http/routes.php @@ -39,6 +39,4 @@ //protected routes with JWT (must be logged in to access any of these routes) $api->group(['middleware' => 'api.auth'], function ($api) { - $api->get('sample/protected', 'LoginController@protectedData'); - }); diff --git a/config/database.php b/config/database.php index edd6425..72fc3dc 100644 --- a/config/database.php +++ b/config/database.php @@ -55,6 +55,7 @@ 'mysql' => [ 'driver' => 'mysql', 'host' => env('DB_HOST', 'localhost'), + 'port' => env('DB_PORT', '3306'), 'database' => env('DB_DATABASE', 'forge'), 'username' => env('DB_USERNAME', 'forge'), 'password' => env('DB_PASSWORD', ''), diff --git a/database/migrations/2014_10_12_000000_create_users_table.php b/database/migrations/2014_10_12_000000_create_users_table.php index 65d3d08..395e676 100644 --- a/database/migrations/2014_10_12_000000_create_users_table.php +++ b/database/migrations/2014_10_12_000000_create_users_table.php @@ -17,7 +17,6 @@ public function up() $table->string('name'); $table->string('email')->unique(); $table->string('password', 60); - $table->rememberToken(); $table->timestamps(); }); }