Added missing AuthController
This commit is contained in:
parent
35cf687e14
commit
143d3bc82f
3 changed files with 135 additions and 27 deletions
35
motorradig/.idea/workspace.xml
generated
35
motorradig/.idea/workspace.xml
generated
|
|
@ -2,25 +2,9 @@
|
||||||
<project version="4">
|
<project version="4">
|
||||||
<component name="ChangeListManager">
|
<component name="ChangeListManager">
|
||||||
<list default="true" id="eed7de89-f054-487c-bf59-36356758942b" name="Default Changelist" comment="">
|
<list default="true" id="eed7de89-f054-487c-bf59-36356758942b" name="Default Changelist" comment="">
|
||||||
<change afterPath="$PROJECT_DIR$/resources/js/routes.js" afterDir="false" />
|
<change afterPath="$PROJECT_DIR$/app/Http/Controllers/AuthController.php" afterDir="false" />
|
||||||
<change afterPath="$PROJECT_DIR$/resources/js/store.js" afterDir="false" />
|
|
||||||
<change afterPath="$PROJECT_DIR$/resources/js/views/Main.vue" afterDir="false" />
|
|
||||||
<change afterPath="$PROJECT_DIR$/resources/js/views/auth/Login.vue" afterDir="false" />
|
|
||||||
<change afterPath="$PROJECT_DIR$/resources/js/views/auth/Register.vue" afterDir="false" />
|
|
||||||
<change afterPath="$PROJECT_DIR$/resources/js/views/components/modal.js" afterDir="false" />
|
|
||||||
<change afterPath="$PROJECT_DIR$/resources/views/main.blade.php" afterDir="false" />
|
|
||||||
<change beforePath="$PROJECT_DIR$/.idea/motorradig.iml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/motorradig.iml" afterDir="false" />
|
|
||||||
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
|
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
|
||||||
<change beforePath="$PROJECT_DIR$/package-lock.json" beforeDir="false" afterPath="$PROJECT_DIR$/package-lock.json" afterDir="false" />
|
<change beforePath="$PROJECT_DIR$/routes/api.php" beforeDir="false" afterPath="$PROJECT_DIR$/routes/api.php" afterDir="false" />
|
||||||
<change beforePath="$PROJECT_DIR$/package.json" beforeDir="false" afterPath="$PROJECT_DIR$/package.json" afterDir="false" />
|
|
||||||
<change beforePath="$PROJECT_DIR$/public/js/app.js" beforeDir="false" afterPath="$PROJECT_DIR$/public/js/app.js" afterDir="false" />
|
|
||||||
<change beforePath="$PROJECT_DIR$/public/js/modal.js" beforeDir="false" afterPath="$PROJECT_DIR$/public/js/modal.js" afterDir="false" />
|
|
||||||
<change beforePath="$PROJECT_DIR$/resources/js/app.js" beforeDir="false" afterPath="$PROJECT_DIR$/resources/js/app.js" afterDir="false" />
|
|
||||||
<change beforePath="$PROJECT_DIR$/resources/js/vue/components/modal.js" beforeDir="false" />
|
|
||||||
<change beforePath="$PROJECT_DIR$/resources/views/home.blade.php" beforeDir="false" />
|
|
||||||
<change beforePath="$PROJECT_DIR$/resources/views/slider_backup.blade.php" beforeDir="false" />
|
|
||||||
<change beforePath="$PROJECT_DIR$/resources/views/welcome.blade.php" beforeDir="false" />
|
|
||||||
<change beforePath="$PROJECT_DIR$/routes/web.php" beforeDir="false" afterPath="$PROJECT_DIR$/routes/web.php" afterDir="false" />
|
|
||||||
</list>
|
</list>
|
||||||
<option name="SHOW_DIALOG" value="false" />
|
<option name="SHOW_DIALOG" value="false" />
|
||||||
<option name="HIGHLIGHT_CONFLICTS" value="true" />
|
<option name="HIGHLIGHT_CONFLICTS" value="true" />
|
||||||
|
|
@ -141,7 +125,7 @@
|
||||||
<component name="PropertiesComponent">
|
<component name="PropertiesComponent">
|
||||||
<property name="ASKED_ADD_EXTERNAL_FILES" value="true" />
|
<property name="ASKED_ADD_EXTERNAL_FILES" value="true" />
|
||||||
<property name="WebServerToolWindowFactoryState" value="false" />
|
<property name="WebServerToolWindowFactoryState" value="false" />
|
||||||
<property name="last_opened_file_path" value="$PROJECT_DIR$/resources/js" />
|
<property name="last_opened_file_path" value="$PROJECT_DIR$/app/Http/Controllers" />
|
||||||
<property name="node.js.detected.package.eslint" value="true" />
|
<property name="node.js.detected.package.eslint" value="true" />
|
||||||
<property name="node.js.detected.package.tslint" value="true" />
|
<property name="node.js.detected.package.tslint" value="true" />
|
||||||
<property name="node.js.path.for.package.eslint" value="project" />
|
<property name="node.js.path.for.package.eslint" value="project" />
|
||||||
|
|
@ -151,16 +135,16 @@
|
||||||
<property name="nodejs_package_manager_path" value="yarn" />
|
<property name="nodejs_package_manager_path" value="yarn" />
|
||||||
</component>
|
</component>
|
||||||
<component name="RecentsManager">
|
<component name="RecentsManager">
|
||||||
|
<key name="MoveFile.RECENT_KEYS">
|
||||||
|
<recent name="$PROJECT_DIR$/resources/views" />
|
||||||
|
<recent name="$PROJECT_DIR$/public" />
|
||||||
|
</key>
|
||||||
<key name="CopyFile.RECENT_KEYS">
|
<key name="CopyFile.RECENT_KEYS">
|
||||||
|
<recent name="$PROJECT_DIR$/app/Http/Controllers" />
|
||||||
<recent name="$PROJECT_DIR$/resources/js" />
|
<recent name="$PROJECT_DIR$/resources/js" />
|
||||||
<recent name="$PROJECT_DIR$/resources/js/views" />
|
<recent name="$PROJECT_DIR$/resources/js/views" />
|
||||||
<recent name="$PROJECT_DIR$/resources/js/vue" />
|
<recent name="$PROJECT_DIR$/resources/js/vue" />
|
||||||
<recent name="$PROJECT_DIR$/resources/js/vue/components" />
|
<recent name="$PROJECT_DIR$/resources/js/vue/components" />
|
||||||
<recent name="$PROJECT_DIR$/public/images" />
|
|
||||||
</key>
|
|
||||||
<key name="MoveFile.RECENT_KEYS">
|
|
||||||
<recent name="$PROJECT_DIR$/resources/views" />
|
|
||||||
<recent name="$PROJECT_DIR$/public" />
|
|
||||||
</key>
|
</key>
|
||||||
</component>
|
</component>
|
||||||
<component name="RunManager" selected="Node.js.app.js">
|
<component name="RunManager" selected="Node.js.app.js">
|
||||||
|
|
@ -222,7 +206,8 @@
|
||||||
<workItem from="1575619993126" duration="703000" />
|
<workItem from="1575619993126" duration="703000" />
|
||||||
<workItem from="1575965115540" duration="593000" />
|
<workItem from="1575965115540" duration="593000" />
|
||||||
<workItem from="1575966299565" duration="383000" />
|
<workItem from="1575966299565" duration="383000" />
|
||||||
<workItem from="1579075615585" duration="7457000" />
|
<workItem from="1579075615585" duration="8354000" />
|
||||||
|
<workItem from="1579595422946" duration="450000" />
|
||||||
</task>
|
</task>
|
||||||
<servers />
|
<servers />
|
||||||
</component>
|
</component>
|
||||||
|
|
|
||||||
97
motorradig/app/Http/Controllers/AuthController.php
Normal file
97
motorradig/app/Http/Controllers/AuthController.php
Normal file
|
|
@ -0,0 +1,97 @@
|
||||||
|
<?php
|
||||||
|
namespace App\Http\Controllers;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
use Illuminate\Support\Facades\Hash;
|
||||||
|
use Log;
|
||||||
|
use Auth;
|
||||||
|
use Validator;
|
||||||
|
use App\User;
|
||||||
|
|
||||||
|
class AuthController extends Controller {
|
||||||
|
/**
|
||||||
|
* Register a new user
|
||||||
|
*/
|
||||||
|
public function register(Request $request)
|
||||||
|
{
|
||||||
|
$v = Validator::make($request->all(), [
|
||||||
|
'name' => 'required|min:3',
|
||||||
|
'email' => 'required|email|unique:users',
|
||||||
|
'password' => 'required|min:3|confirmed',
|
||||||
|
]);
|
||||||
|
if ($v->fails()) {
|
||||||
|
return response()->json([
|
||||||
|
'status' => 'error',
|
||||||
|
'errors' => $v->errors()
|
||||||
|
], 422);
|
||||||
|
}
|
||||||
|
$user = new User();
|
||||||
|
$user->name = $request->name;
|
||||||
|
$user->email = $request->email;
|
||||||
|
$user->password = bcrypt($request->password);
|
||||||
|
$user->save();
|
||||||
|
return response()->json(['status' => 'success'], 200);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Login user and return a token
|
||||||
|
*/
|
||||||
|
public function login(Request $request)
|
||||||
|
{
|
||||||
|
Log::info("Handling logging request");
|
||||||
|
Log::info($request);
|
||||||
|
|
||||||
|
$user = User::where('email',$request->email)->first();
|
||||||
|
Log::info("User: ".$user->id." => ".$user->name);
|
||||||
|
|
||||||
|
$credentials = $request->only('email', 'password');
|
||||||
|
if ($token = $this->guard()->attempt($credentials)) {
|
||||||
|
return response()->json(['status' => 'success'], 200)->header('Authorization', $token);
|
||||||
|
}
|
||||||
|
return response()->json(['error' => 'login_error'], 401);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Logout User
|
||||||
|
*/
|
||||||
|
public function logout()
|
||||||
|
{
|
||||||
|
$this->guard()->logout();
|
||||||
|
return response()->json([
|
||||||
|
'status' => 'success',
|
||||||
|
'msg' => 'Logged out Successfully.'
|
||||||
|
], 200);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get authenticated user
|
||||||
|
*/
|
||||||
|
public function user(Request $request)
|
||||||
|
{
|
||||||
|
$user = User::find(Auth::user()->id);
|
||||||
|
return response()->json([
|
||||||
|
'status' => 'success',
|
||||||
|
'data' => $user
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Refresh JWT token
|
||||||
|
*/
|
||||||
|
public function refresh()
|
||||||
|
{
|
||||||
|
if ($token = $this->guard()->refresh()) {
|
||||||
|
return response()
|
||||||
|
->json(['status' => 'successs'], 200)
|
||||||
|
->header('Authorization', $token);
|
||||||
|
}
|
||||||
|
return response()->json(['error' => 'refresh_token_error'], 401);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return auth guard
|
||||||
|
*/
|
||||||
|
private function guard()
|
||||||
|
{
|
||||||
|
return Auth::guard();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -13,6 +13,32 @@ use Illuminate\Http\Request;
|
||||||
|
|
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
Route::middleware('auth:api')->get('/user', function (Request $request) {
|
Route::prefix('v1')->group(function () {
|
||||||
return $request->user();
|
Route::prefix('auth')->group(function () {
|
||||||
|
// Below mention routes are public, user can access those without any restriction.
|
||||||
|
|
||||||
|
// Create New User
|
||||||
|
Route::post('register', 'AuthController@register');
|
||||||
|
|
||||||
|
// Login User
|
||||||
|
Route::post('login', 'AuthController@login');
|
||||||
|
|
||||||
|
// Refresh the JWT Token
|
||||||
|
Route::get('refresh', 'AuthController@refresh');
|
||||||
|
|
||||||
|
// Below mention routes are available only for the authenticated users.
|
||||||
|
Route::middleware('auth:api')->group(function () {
|
||||||
|
|
||||||
|
//Route::middleware('auth.jwt')->group(function () {
|
||||||
|
// Get user info
|
||||||
|
Route::get('user', 'AuthController@user');
|
||||||
|
|
||||||
|
// Logout user from application
|
||||||
|
Route::post('logout', 'AuthController@logout');
|
||||||
|
|
||||||
|
//TEST
|
||||||
|
Route::post('showme', 'UserController@showme');
|
||||||
|
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue