Skip to content

Commit

Permalink
Initial commit for version 2
Browse files Browse the repository at this point in the history
  • Loading branch information
johndavedecano committed Apr 16, 2018
0 parents commit 801ce21
Show file tree
Hide file tree
Showing 94 changed files with 8,163 additions and 0 deletions.
41 changes: 41 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
APP_ENV=local
APP_KEY=
APP_DEBUG=true
APP_LOG_LEVEL=debug
APP_URL=http://localhost

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret

BROADCAST_DRIVER=log
CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

MAIL_DRIVER=smtp
MAIL_HOST=mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null

PUSHER_APP_ID=
PUSHER_KEY=
PUSHER_SECRET=

API_PREFIX=api
API_SUBTYPE=app
API_VERSION=v1

SIGN_UP_RELEASE_TOKEN=false
PASSWORD_RESET_RELEASE_TOKEN=false

JWT_SECRET=my-dummy-token
3 changes: 3 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
* text=auto
*.css linguist-vendored
*.scss linguist-vendored
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/node_modules
/public/storage
/storage/*.key
/vendor
/.idea
Homestead.json
Homestead.yaml
.env
43 changes: 43 additions & 0 deletions app/Api/V1/Controllers/ForgotPasswordController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace App\Api\V1\Controllers;

use App\User;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Password;
use App\Api\V1\Requests\ForgotPasswordRequest;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

class ForgotPasswordController extends Controller
{
public function sendResetEmail(ForgotPasswordRequest $request)
{
$user = User::where('email', '=', $request->get('email'))->first();

if(!$user) {
throw new NotFoundHttpException();
}

$broker = $this->getPasswordBroker();
$sendingResponse = $broker->sendResetLink($request->only('email'));

if($sendingResponse !== Password::RESET_LINK_SENT) {
throw new HttpException(500);
}

return response()->json([
'status' => 'ok'
], 200);
}

/**
* Get the broker to be used during password reset.
*
* @return \Illuminate\Contracts\Auth\PasswordBroker
*/
private function getPasswordBroker()
{
return Password::broker();
}
}
44 changes: 44 additions & 0 deletions app/Api/V1/Controllers/LoginController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace App\Api\V1\Controllers;

use Symfony\Component\HttpKernel\Exception\HttpException;
use Tymon\JWTAuth\JWTAuth;
use App\Http\Controllers\Controller;
use App\Api\V1\Requests\LoginRequest;
use Tymon\JWTAuth\Exceptions\JWTException;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Auth;

class LoginController extends Controller
{
/**
* Log the user in
*
* @param LoginRequest $request
* @param JWTAuth $JWTAuth
* @return \Illuminate\Http\JsonResponse
*/
public function login(LoginRequest $request, JWTAuth $JWTAuth)
{
$credentials = $request->only(['email', 'password']);

try {
$token = Auth::guard()->attempt($credentials);

if(!$token) {
throw new AccessDeniedHttpException();
}

} catch (JWTException $e) {
throw new HttpException(500);
}

return response()
->json([
'status' => 'ok',
'token' => $token,
'expires_in' => Auth::guard()->factory()->getTTL() * 60
]);
}
}
32 changes: 32 additions & 0 deletions app/Api/V1/Controllers/LogoutController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace App\Api\V1\Controllers;

use App\Http\Controllers\Controller;
use Auth;

class LogoutController extends Controller
{
/**
* Create a new AuthController instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('auth:api', []);
}

/**
* Log the user out (Invalidate the token)
*
* @return \Illuminate\Http\JsonResponse
*/
public function logout()
{
Auth::guard()->logout();

return response()
->json(['message' => 'Successfully logged out']);
}
}
30 changes: 30 additions & 0 deletions app/Api/V1/Controllers/RefreshController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace App\Api\V1\Controllers;

use Symfony\Component\HttpKernel\Exception\HttpException;
use Tymon\JWTAuth\JWTAuth;
use App\Http\Controllers\Controller;
use App\Api\V1\Requests\LoginRequest;
use Tymon\JWTAuth\Exceptions\JWTException;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Auth;

class RefreshController extends Controller
{
/**
* Refresh a token.
*
* @return \Illuminate\Http\JsonResponse
*/
public function refresh()
{
$token = Auth::guard()->refresh();

return response()->json([
'status' => 'ok',
'token' => $token,
'expires_in' => Auth::guard()->factory()->getTTL() * 60
]);
}
}
76 changes: 76 additions & 0 deletions app/Api/V1/Controllers/ResetPasswordController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

namespace App\Api\V1\Controllers;

use Config;
use App\User;
use Tymon\JWTAuth\JWTAuth;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Password;
use App\Api\V1\Requests\ResetPasswordRequest;
use Symfony\Component\HttpKernel\Exception\HttpException;

class ResetPasswordController extends Controller
{
public function resetPassword(ResetPasswordRequest $request, JWTAuth $JWTAuth)
{
$response = $this->broker()->reset(
$this->credentials($request), function ($user, $password) {
$this->reset($user, $password);
}
);

if($response !== Password::PASSWORD_RESET) {
throw new HttpException(500);
}

if(!Config::get('boilerplate.reset_password.release_token')) {
return response()->json([
'status' => 'ok',
]);
}

$user = User::where('email', '=', $request->get('email'))->first();

return response()->json([
'status' => 'ok',
'token' => $JWTAuth->fromUser($user)
]);
}

/**
* Get the broker to be used during password reset.
*
* @return \Illuminate\Contracts\Auth\PasswordBroker
*/
public function broker()
{
return Password::broker();
}

/**
* Get the password reset credentials from the request.
*
* @param ResetPasswordRequest $request
* @return array
*/
protected function credentials(ResetPasswordRequest $request)
{
return $request->only(
'email', 'password', 'password_confirmation', 'token'
);
}

/**
* Reset the given user's password.
*
* @param \Illuminate\Contracts\Auth\CanResetPassword $user
* @param string $password
* @return void
*/
protected function reset($user, $password)
{
$user->password = $password;
$user->save();
}
}
33 changes: 33 additions & 0 deletions app/Api/V1/Controllers/SignUpController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace App\Api\V1\Controllers;

use Config;
use App\User;
use Tymon\JWTAuth\JWTAuth;
use App\Http\Controllers\Controller;
use App\Api\V1\Requests\SignUpRequest;
use Symfony\Component\HttpKernel\Exception\HttpException;

class SignUpController extends Controller
{
public function signUp(SignUpRequest $request, JWTAuth $JWTAuth)
{
$user = new User($request->all());
if(!$user->save()) {
throw new HttpException(500);
}

if(!Config::get('boilerplate.sign_up.release_token')) {
return response()->json([
'status' => 'ok'
], 201);
}

$token = $JWTAuth->fromUser($user);
return response()->json([
'status' => 'ok',
'token' => $token
], 201);
}
}
34 changes: 34 additions & 0 deletions app/Api/V1/Controllers/UserController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace App\Api\V1\Controllers;

use Symfony\Component\HttpKernel\Exception\HttpException;
use Tymon\JWTAuth\JWTAuth;
use App\Http\Controllers\Controller;
use App\Api\V1\Requests\LoginRequest;
use Tymon\JWTAuth\Exceptions\JWTException;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Auth;

class UserController extends Controller
{
/**
* Create a new AuthController instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('auth:api', []);
}

/**
* Get the authenticated User
*
* @return \Illuminate\Http\JsonResponse
*/
public function me()
{
return response()->json(Auth::guard()->user());
}
}
19 changes: 19 additions & 0 deletions app/Api/V1/Requests/ForgotPasswordRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace App\Api\V1\Requests;

use Config;
use Dingo\Api\Http\FormRequest;

class ForgotPasswordRequest extends FormRequest
{
public function rules()
{
return Config::get('boilerplate.forgot_password.validation_rules');
}

public function authorize()
{
return true;
}
}
19 changes: 19 additions & 0 deletions app/Api/V1/Requests/LoginRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace App\Api\V1\Requests;

use Config;
use Dingo\Api\Http\FormRequest;

class LoginRequest extends FormRequest
{
public function rules()
{
return Config::get('boilerplate.login.validation_rules');
}

public function authorize()
{
return true;
}
}
Loading

0 comments on commit 801ce21

Please sign in to comment.