"라라벨 TokenGuard.php"의 두 판 사이의 차이

잔글 (Jmnote님이 라라벨 TokenGuard 문서를 라라벨 TokenGuard.php 문서로 이동했습니다)
잔글 (봇: 자동으로 텍스트 교체 (-==참고 자료== +==참고==))
79번째 줄: 79번째 줄:
* [[라라벨 AuthManager]]
* [[라라벨 AuthManager]]


==참고 자료==
==참고==
* https://laravel.com/api/5.2/Illuminate/Auth/TokenGuard.html
* https://laravel.com/api/5.2/Illuminate/Auth/TokenGuard.html
* https://github.com/laravel/framework/blob/5.2/src/Illuminate/Auth/TokenGuard.php
* https://github.com/laravel/framework/blob/5.2/src/Illuminate/Auth/TokenGuard.php


[[분류: Illuminate\Auth]]
[[분류: Illuminate\Auth]]

2017년 7월 11일 (화) 02:14 판

1 개요

라라벨 TokenGuard
<?php

namespace Illuminate\Auth;

use Illuminate\Http\Request;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Contracts\Auth\UserProvider;

class TokenGuard implements Guard
{
    use GuardHelpers;

    protected $request;
    protected $inputKey;
    protected $storageKey;

    public function __construct(UserProvider $provider, Request $request)
    {
        $this->request = $request;
        $this->provider = $provider;
        $this->inputKey = 'api_token';
        $this->storageKey = 'api_token';
    }

    public function user()
    {
        if (! is_null($this->user)) {
            return $this->user;
        }
        $user = null;
        $token = $this->getTokenForRequest();
        if (! empty($token)) {
            $user = $this->provider->retrieveByCredentials(
                [$this->storageKey => $token]
            );
        }
        return $this->user = $user;
    }

    protected function getTokenForRequest()
    {
        $token = $this->request->input($this->inputKey);
        if (empty($token)) {
            $token = $this->request->bearerToken();
        }
        if (empty($token)) {
            $token = $this->request->getPassword();
        }
        return $token;
    }

    public function validate(array $credentials = [])
    {
        if (empty($credentials[$this->inputKey])) {
            return false;
        }
        $credentials = [$this->storageKey => $credentials[$this->inputKey]];
        if ($this->provider->retrieveByCredentials($credentials)) {
            return true;
        }
        return false;
    }

    public function setRequest(Request $request)
    {
        $this->request = $request;

        return $this;
    }
}

2 같이 보기

3 참고

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}