Skip to content
  • P
    Projects
  • G
    Groups
  • S
    Snippets
  • Help

semour / semour_web

  • This project
    • Loading...
  • Sign in
Go to a project
  • Project
  • Repository
  • Issues 0
  • Merge Requests 0
  • Pipelines
  • Wiki
  • Snippets
  • Settings
  • Activity
  • Graph
  • Charts
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
  • Files
  • Commits
  • Branches
  • Tags
  • Contributors
  • Graph
  • Compare
  • Charts
Find file
BlameHistoryPermalink
Switch branch/tag
  • semour_web
  • app
  • Http
  • Controllers
  • Api
  • AuthApiController.php
  • 杨树贤's avatar
    注册新增字段 · 4e77187f
    杨树贤 committed 2 years ago
    4e77187f
AuthApiController.php 4.7 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
<?php

namespace App\Http\Controllers\Api;

use App\Http\Requests\UserRegister;
use App\Models\UserModel;
use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Foundation\Auth\ThrottlesLogins;
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
{

    use ThrottlesLogins, RegistersUsers;

    public function register(UserRegister $request)
    {
        $email = $request->input('email');
        if (UserModel::where('email', $email)->exists()) {
            return $this->setError('Email has been taken');
        }

        //判断邮箱验证码
        $redisKey = 'sem_email_code_register_' . $email;
        $cachedEmailCode = Redis::get($redisKey);
        if ($cachedEmailCode != $request->input('email_code')) {
            return $this->setError('Email code invalid');
        }
        $userId = UserModel::createUser($request->all());

        \Auth::loginUsingId($userId);
        return $this->setSuccess('Register success');
    }


    public function login(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'email' => 'required|string|email|max:255',
            'password' => 'required|string|min:8',
        ], [
            'password.min' => 'Password must be at least 8 characters long.'
        ]);

        if ($validator->fails()) {
            return $this->setError($validator->errors()->first());
        }

        $userExists = UserModel::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');
        }

        $this->incrementLoginAttempts($request);

        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');
        $oldPassword = $request->get('old_password');
        $userId = Auth::user()->id;
        $hashedPassword = UserModel::where('id', $userId)->value('password');
        if (!Hash::check($oldPassword, $hashedPassword)) {
            return $this->setError('Wrong Password!');
        }

        $user = UserModel::find($userId);
        $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');

        $validator = Validator::make($request->all(), [
            'email' => 'required|email',
        ]);
        if ($validator->fails()) {
            return $this->setError($validator->errors()->first());
        }
        $info = UserModel::where('email', $email)->first();
        if ($info && $type == 'register') {
            return $this->setError('This email had been registered');
        }

        //发送验证码
        $code = mt_rand(1000, 9999);
        $redisKey = 'sem_email_code_' . $type . '_' . $email;
        if (Redis::get($redisKey)) {
            return $this->setError('Email code had been sent');
        }
        Redis::set($redisKey, $code);
        Redis::expire($redisKey, 60);
        $subject = config('mail.from.name');
        $msg = 'Email Code:' . $code . '.';
        return $this->setSuccessData($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');
    }

}