<commit_before><?php

declare(strict_types=1);

/*
 * +----------------------------------------------------------------------+
 * |                          ThinkSNS Plus                               |
 * +----------------------------------------------------------------------+
 * | Copyright (c) 2017 <NAME>ang Technology Co., Ltd.     |
 * +----------------------------------------------------------------------+
 * | This source file is subject to version 2.0 of the Apache license,    |
 * | that is bundled with this package in the file LICENSE, and is        |
 * | available through the world-wide-web at the following url:           |
 * | http://www.apache.org/licenses/LICENSE-2.0.html                      |
 * +----------------------------------------------------------------------+
 * | Author: Slim Kit Group <<EMAIL>>                          |
 * | Homepage: www.thinksns.com                                           |
 * +----------------------------------------------------------------------+
 */

namespace Zhiyi\Plus\Http\Controllers\APIs\V2;

use Illuminate\Http\Request;
use function Zhiyi\Plus\username;
use Illuminate\Http\JsonResponse;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Support\Facades\Auth;

class AuthController extends Controller
{
    /**
     * Create a new AuthController instance.
     *
     * @author <NAME> <<EMAIL>>
     */
    public function __construct()
    {
        $this->middleware('auth:api', ['except' => ['login', 'refresh']]);
    }

    /**
     * Get the guard to be used during authentication.
     *
     * @return \Illuminate\Contracts\Auth\Guard
     */
    public function guard(): Guard
    {
        return Auth::guard('api');
    }

    /**
     * Get a JWT token via given credentials.
     *
     * @param \Illuminate\Http\Request $request
     * @return \Illuminate\Http\JsonResponse
     * @author <NAME> <<EMAIL>>
     */
    public function login(Request $request): JsonResponse
    {
        $login = (string) $request->input('login', '');
        $credentials = [
            username($login) => $login,
            'password' => $request->input('password', ''),
        ];

        if ($token = $this->guard()->attempt($credentials)) {
            return $this->respondWithToken($token);
        }

        return $this->response()->json(['message' => '账号或密码不正确'], 422);
    }

    /**
     * Log the user out (Invalidate the token).
     *
     * @return \Illuminate\Http\JsonResponse
     * @author <NAME> <<EMAIL>>
     */
    public function logout(): JsonResponse
    {
        $this->guard()->logout();

        return $this->response()->json(['message' => '退出成功']);
    }

    /**
     * Refresh a token.
     *
     * @return \Illuminate\Http\JsonResponse
     * @author <NAME> <<EMAIL>>
     */<commit_msg>feat: 支持验证码登录<commit_after><?php

declare(strict_types=1);

/*
 * +----------------------------------------------------------------------+
 * |                          ThinkSNS Plus                               |
 * +----------------------------------------------------------------------+
 * | Copyright (c) 2017 <NAME> Technology Co., Ltd.     |
 * +----------------------------------------------------------------------+
 * | This source file is subject to version 2.0 of the Apache license,    |
 * | that is bundled with this package in the file LICENSE, and is        |
 * | available through the world-wide-web at the following url:           |
 * | http://www.apache.org/licenses/LICENSE-2.0.html                      |
 * +----------------------------------------------------------------------+
 * | Author: Slim Kit Group <<EMAIL>>                          |
 * | Homepage: www.thinksns.com                                           |
 * +----------------------------------------------------------------------+
 */

namespace Zhiyi\Plus\Http\Controllers\APIs\V2;

use Zhiyi\Plus\Models\User;
use Illuminate\Http\Request;
use function Zhiyi\Plus\username;
use Illuminate\Http\JsonResponse;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Support\Facades\Auth;
use Zhiyi\Plus\Models\VerificationCode;

class AuthController extends Controller
{
    /**
     * Create a new AuthController instance.
     *
     * @author <NAME> <<EMAIL>>
     */
    public function __construct()
    {
        $this->middleware('auth:api', ['except' => ['login', 'refresh']]);
    }

    /**
     * Get the guard to be used during authentication.
     *
     * @return \Illuminate\Contracts\Auth\Guard
     */
    public function guard(): Guard
    {
        return Auth::guard('api');
    }

    /**
     * Get a JWT token via given credentials.
     *
     * @param \Illuminate\Http\Request $request
     * @return \Illuminate\Http\JsonResponse
     * @author <NAME> <<EMAIL>>
     */
    public function login(Request $request): JsonResponse
    {
        $login = (string) $request->input('login', '');
        $code = $request->input('verifiable_code');
        $field = username($login);

        if ($code !== null && in_array($field, ['phone', 'email'])) {
            $verify = VerificationCode::where('account', $login)
                ->where('channel', $field == 'phone' ? 'sms' : 'mail')
                ->where('code', $code)
                ->byValid(120)
                ->orderby('id', 'desc')
                ->first();

            if (! $verify) {
                return $this->response()->json(['message' => '验证码错误或者已失效'], 422);
            }

            $verify->delete();

            if ($user = User::where($field, $login)->first()) {
                return $this->respondWithToken($this->guard()->login($user));
            }

            return $this->response()->json([
                'message' => sprintf('%s还没有注册', $field == 'phone' ? '手机号' : '邮箱')
            ], 422);
        }

        $credentials = [
            $field => $login,
            'password' => $request->input('password', ''),
        ];

        if ($token = $this->guard()->attempt($credentials)) {
            return $this->respondWithToken($token);
        }

        return $this->response()->json(['message' => '账号或密码不正确'], 422);
    }

    /**
     * Log the user out (Invalidate the token).
     *
     * @return \Illuminate\Http\JsonResponse
     * @author <NAME> <<EMAIL>>
     */
    public function logout(): JsonResponse
    {
        $this->guard()->logout();

        return $this->response()->json(['message' => '退出成功']);
    }

    /**
     * Refresh a token.
     *
     * @return \Illuminate\Http\JsonResponse
     * @author <NAME> <<EMAIL>>
     */
{'fraction_non_alphanumeric': 0.2492003046458492, 'fraction_numerical': 0.005331302361005331, 'mean_word_length': 1.4237726098191215, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 84, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '362990', 'n_tokens_mistral': 1767, 'n_tokens_neox': 1766, 'n_words': 379}