Firebase Auth JSPHP

Firebase Auth JSPHP,第1张

Firebase Auth JS / PHP

我不得不承认,firebase文档和示例的复杂性以及不同的服务使我非常困惑,以至于我认为只能通过Javascript进行Web身份验证。错了
至少对于我来说,我只是 使用电子邮件和密码登录检索Json Web令牌(JWT)
,以对Firebase云功能的所有调用进行签名。无需处理奇怪的Ajax请求或通过Javascript设置令牌cookie,我只需要调用FirebaseAuth REST API

这是使用Fatfreeframework的最小情况:

登录表单

<form action="/auth" method="post">    <input name="email">    <input name="password">    <input type="submit"></form>

路线

$f3->route('POST /auth', 'App->auth');

控制者

class App{    function auth()    {        $email = $this->f3->get('POST.email');        $password = $this->f3->get('POST.password');        $apiKey = 'API_KEY'; // see https://firebase.google.com/docs/web/setup        $auth = new Auth($apiKey);        $result = $auth->login($email,$password);        if($result['success']){ $this->f3->set('cookie.token',$result['idToken']); $this->f3->reroute('/dashboard');        }else{ $this->f3->clear('cookie.token'); $this->f3->reroute('/');        }    }}

<?phpuse GuzzleHttpClient;class Auth{    protected $apiKey;    public function __construct($apiKey){        $this->apiKey = $apiKey;    }    public function login($email,$password)    {        $client = new Client();        // Create a POST request using google api        $key = $this->apiKey;        $responsee = $client->request( 'POST', 'https://www.googleapis.com/identitytoolkit/v3/relyingparty/verifyPassword?key=' . $key, [     'headers' => [         'content-type' => 'application/json',         'Accept' => 'application/json'     ],     'body' => json_enpre([         'email' => $email,         'password' => $password,         'returnSecureToken' => true     ]),     'exceptions' => false ]        );        $body = $responsee->getBody();        $js = json_depre($body);        if (isset($js->error)) { return [     'success' => false,     'message' => $js->error->message ];        } else { return [     'success' => true,     'localId' => $js->localId,     'idToken' => $js->idToken,     'email' => $js->email,     'refreshToken' => $js->refreshToken,     'expiresIn' => $js->expiresIn, ];        }    }}

学分



欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5016397.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-14
下一篇 2022-11-15

发表评论

登录后才能评论

评论列表(0条)

保存