Commit 1a52f4a5 by 杨树贤

重置秘密和邮箱相关接口

parent 069ec102
......@@ -19,8 +19,8 @@ QUEUE_CONNECTION=sync
SESSION_DRIVER=file
SESSION_LIFETIME=120
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_HOST=192.168.1.235
REDIS_PASSWORD=icDb29mLy2s
REDIS_PORT=6379
MAIL_MAILER=smtp
......
......@@ -2,11 +2,14 @@
<project version="4">
<component name="ChangeListManager">
<list default="true" id="fb90add0-1393-48c2-9f26-72365d42cd03" name="变更" comment="">
<change beforePath="$PROJECT_DIR$/.env" beforeDir="false" afterPath="$PROJECT_DIR$/.env" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/app/Http/Controllers/Api/AuthApiController.php" beforeDir="false" afterPath="$PROJECT_DIR$/app/Http/Controllers/Api/AuthApiController.php" afterDir="false" />
<change beforePath="$PROJECT_DIR$/app/Http/Controllers/Controller.php" beforeDir="false" afterPath="$PROJECT_DIR$/app/Http/Controllers/Controller.php" afterDir="false" />
<change beforePath="$PROJECT_DIR$/app/Http/Kernel.php" beforeDir="false" afterPath="$PROJECT_DIR$/app/Http/Kernel.php" afterDir="false" />
<change beforePath="$PROJECT_DIR$/app/Models/User.php" beforeDir="false" afterPath="$PROJECT_DIR$/app/Models/User.php" afterDir="false" />
<change beforePath="$PROJECT_DIR$/config/database.php" beforeDir="false" afterPath="$PROJECT_DIR$/config/database.php" afterDir="false" />
<change beforePath="$PROJECT_DIR$/routes/api.php" beforeDir="false" afterPath="$PROJECT_DIR$/routes/api.php" afterDir="false" />
<change beforePath="$PROJECT_DIR$/routes/web.php" beforeDir="false" afterPath="$PROJECT_DIR$/routes/web.php" afterDir="false" />
<change beforePath="$PROJECT_DIR$/storage/app/.gitignore" beforeDir="false" afterPath="$PROJECT_DIR$/storage/app/.gitignore" afterDir="false" />
<change beforePath="$PROJECT_DIR$/storage/app/public/.gitignore" beforeDir="false" afterPath="$PROJECT_DIR$/storage/app/public/.gitignore" afterDir="false" />
<change beforePath="$PROJECT_DIR$/storage/framework/.gitignore" beforeDir="false" afterPath="$PROJECT_DIR$/storage/framework/.gitignore" afterDir="false" />
......@@ -185,7 +188,7 @@
<updated>1666170258203</updated>
<workItem from="1666170260162" duration="17108000" />
<workItem from="1666835076791" duration="693000" />
<workItem from="1667266026118" duration="595000" />
<workItem from="1667266026118" duration="8640000" />
</task>
<servers />
</component>
......
......@@ -3,15 +3,13 @@
namespace App\Http\Controllers\Api;
use App\Models\User;
use Illuminate\Auth\Events\Registered;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Auth\RedirectsUsers;
use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Foundation\Auth\ThrottlesLogins;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Facades\Validator;
class AuthApiController extends Controller
......@@ -25,6 +23,8 @@ class AuthApiController extends Controller
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'password' => ['required', 'string', 'min:8', 'confirmed'],
], [
'password.confirmed' => 'Passwords do not match!',
]);
if ($validator->fails()) {
return $this->setError($validator->errors()->first());
......@@ -32,8 +32,8 @@ class AuthApiController extends Controller
$user = User::createUser($request->all());
\Auth::login($user);
return $this->setSuccess('register success');
\Auth::login($user);
return $this->setSuccess('Register success');
}
......@@ -50,14 +50,90 @@ class AuthApiController extends Controller
return $this->setError($validator->errors()->first());
}
$userExists = User::where('email', $request->email)->exists();
if (!$userExists) {
return $this->setError('User dose not exist');
}
if ($this->attemptLogin($request)) {
$request->session()->regenerate();
return $this->setSuccess('login success');
return $this->setSuccess('Login success');
}
$this->incrementLoginAttempts($request);
return $this->setError('login failure');
return $this->setError('Login failure');
}
public function logout(Request $request)
{
Auth::logout();
return $this->setSuccess('Logout success');
}
public function resetPassword(Request $request)
{
$validator = Validator::make($request->all(), [
'old_password' => 'required|min:8',
'password' => ['required', 'string', 'min:8', 'confirmed'],
], [
'old_password.min' => 'Password must be at least 8 characters long.',
'password.min' => 'Password must be at least 8 characters long.',
'password.confirmed' => 'Passwords do not match!',
]);
if ($validator->fails()) {
return $this->setError($validator->errors()->first());
}
$password = $request->get('password');
$match = User::where('password', Hash::make($request->get('old_password')))->exists();
if (!$match) {
return $this->setError('Wrong Password!');
}
$user = new User();
$user->password = Hash::make($password);
$user->update_time = time();
$result = $user->save();
if (!$result) {
return $this->setError('Reset password failed');
}
return $this->setSuccess('Reset password success');
}
//发送邮箱验证码
public function sendEmailCode(Request $request)
{
$email = $request->input('email');
$type = $request->input('type', 'register');
if (!$email) {
return $this->setError('Email address required');
}
$info = User::where('email', $email)->first();
if ($info) {
return $this->setError('This email had been registered');
}
//发送验证码
$code = mt_rand(1000, 9999);
$redisKey = 'sem_email_code_' . $type;
Redis::hset($redisKey, $code);
Redis::expire($redisKey, 60);
$subject = config('mail.from.name');
$msg = 'Email Code:' . $code . '.';
Mail::raw($msg, function ($message) use ($email, $subject) {
$message->to($email)->subject($subject);
});
//错误处理
if (count(Mail::failures())) {
return $this->setError('Email code send failed');
}
return $this->setSuccess('Email code send success');
}
}
......@@ -2,12 +2,73 @@
namespace App\Http\Controllers;
use App\Http\ApiHelper\ApiCode;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Support\Facades\Log;
class Controller extends BaseController
{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
public function setSuccessData($data = [], $count = 0, $code = ApiCode::API_CODE_SUCCESS, $msg = 'ok')
{
$res_data = [
"code" => $code,
"data" => $data,
];
if ($msg) {
$res_data['msg'] = $msg;
}
if ($count) {
$res_data['count'] = $count;
}
return response()->json($res_data);
}
public function setSuccess($msg = '操作成功', $code = ApiCode::API_CODE_SUCCESS, $data = [])
{
$res_data = [
"code" => $code,
"msg" => $msg,
'data' => (object)$data,
];
return response()->json($res_data);
}
public function setError($msg, $code = ApiCode::API_CODE_ERROR, $data = [])
{
$res_data = [
"code" => $code,
"msg" => $msg,
];
if ($data) {
$res_data['data'] = $data;
}
$this->logErr($msg, $code = ApiCode::API_CODE_ERROR, $data = null);
return response()->json($res_data);
}
private function logErr($msg, $code = ApiCode::API_CODE_ERROR, $data = null)
{
$request_uri = $_SERVER['REQUEST_URI'] ?? '';
$path_info = parse_url($request_uri);
$err_info = [
'domain' => $_SERVER['HTTP_HOST'] ?? '',
'interface' => isset($path_info) ? $path_info['path'] : '',
'user_agent' => $_SERVER['HTTP_USER_AGENT'] ?? '',
'ip' => request()->getClientIp(),
'time' => time(),
'other' => '',
'request_params' => $_REQUEST,
'msg' => $msg,
"code" => $code,
"data" => $data
];
Log::error(json_encode($err_info, JSON_UNESCAPED_UNICODE));
}
}
......@@ -2,6 +2,7 @@
namespace App\Http;
use App\Http\Middleware\CheckApiLogin;
use Illuminate\Foundation\Http\Kernel as HttpKernel;
class Kernel extends HttpKernel
......@@ -41,10 +42,11 @@ class Kernel extends HttpKernel
'api' => [
'throttle:60,1',
\Illuminate\Routing\Middleware\SubstituteBindings::class,
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
],
];
......@@ -56,6 +58,7 @@ class Kernel extends HttpKernel
* @var array
*/
protected $routeMiddleware = [
'api.check' => CheckApiLogin::class,
'auth' => \App\Http\Middleware\Authenticate::class,
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
......
<?php
namespace App\Http\Middleware;
use App\Http\ApiHelper\Response;
use Closure;
class CheckApiLogin
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
if (!\Auth::check()) {
$response = [
'code' => 1,
'msg' => 'need login...',
];
return response()->json($response);
}else{
return $next($request);
}
}
}
......@@ -54,7 +54,7 @@ return [
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => 'lie_',
'prefix' => 'sem_',
'prefix_indexes' => true,
'strict' => true,
'engine' => null,
......
......@@ -20,6 +20,10 @@ Route::middleware(['api'])->namespace('Api')->group(function () {
});
//Route::middleware(['auth'])->namespace('Api')->group(function () {
// Route::POST('/user/login', 'AuthApiController@login');
//});
Route::middleware(['api','api.check'])->namespace('Api')->group(function () {
Route::GET('/user/logout', 'AuthApiController@logout');
});
Route::middleware(['api','api.check'])->namespace('Auth')->group(function () {
Route::Any('user/reset_password', 'ResetPasswordController@reset');
});
......@@ -14,7 +14,7 @@ use Illuminate\Support\Facades\Route;
|
*/
//Auth::routes(['verify' => true]);
Auth::routes(['verify' => true]);
Route::middleware(['auth'])->group(function () {
Route::get('/test', function () {
......
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 sign in to comment