PHP Firebase help - Set up JWT

后端 未结 2 2024
再見小時候
再見小時候 2021-02-08 17:26

On my server I am running a few PHP files that read my Firebase Realtime Database. According to Firebase\'s documents I need to set up custom token to get my Firebase PHP Client

2条回答
  •  鱼传尺愫
    2021-02-08 17:51

    firebase/php-jwt

    Source Link with Angular App

    action;
    
    
    // Login section
    if ($action == 'login') {
    
        $email = $request->email;
        $password = $request->password; 
    
            //A dummy credential match.. you should have some SQl queries to match from databases
            if($email == "freaky@jolly.com" && $password == "12345678")
            {
                $iat = time(); // time of token issued at
                $nbf = $iat + 10; //not before in seconds
                $exp = $iat + 60; // expire time of token in seconds
    
                $token = array(
                    "iss" => "http://example.org",
                    "aud" => "http://example.com",
                    "iat" => $iat,
                    "nbf" => $nbf,
                    "exp" => $exp,
                    "data" => array(
                            "id" => 11,
                            "email" => $email
                    )
                );
    
                http_response_code(200);
    
                $jwt = JWT::encode($token, SECRET_KEY);
    
    
                $data_insert=array(
                    'access_token' => $jwt,                                 
                    'id'   => '007',
                    'name' => 'Jolly',
                    'time' => time(),
                    'username' => 'FreakyJolly', 
                    'email' => 'contact@freakyjolly.com', 
                    'status' => "success",
                    'message' => "Successfully Logged In"
                );
    
    
            }else{
                $data_insert=array(
                    "data" => "0",
                    "status" => "invalid",
                    "message" => "Invalid Request"
                );  
            }   
    
    }
    // Get Dashboard stuff
    else if($action == 'stuff'){
    
        $authHeader = $_SERVER['HTTP_AUTHORIZATION'];
        $temp_header = explode(" ", $authHeader);
        $jwt = $temp_header[1];
    
        try {
            JWT::$leeway = 10;
            $decoded = JWT::decode($jwt, SECRET_KEY, array(ALGORITHM));
    
            // Access is granted. Add code of the operation here 
    
            $data_from_server = '{"Coords":[{"Accuracy":"65","Latitude":"53.277720488429026","Longitude":"-9.012038778269686","Timestamp":"Fri Jul 05 2013 11:59:34 GMT+0100 (IST)"},{"Accuracy":"65","Latitude":"53.277720488429026","Longitude":"-9.012038778269686","Timestamp":"Fri Jul 05 2013 11:59:34 GMT+0100 (IST)"},{"Accuracy":"65","Latitude":"53.27770755361785","Longitude":"-9.011979642121824","Timestamp":"Fri Jul 05 2013 12:02:09 GMT+0100 (IST)"},{"Accuracy":"65","Latitude":"53.27769091555766","Longitude":"-9.012051410095722","Timestamp":"Fri Jul 05 2013 12:02:17 GMT+0100 (IST)"},{"Accuracy":"65","Latitude":"53.27769091555766","Longitude":"-9.012051410095722","Timestamp":"Fri Jul 05 2013 12:02:17 GMT+0100 (IST)"}]}';
    
    
            $data_insert=array(
                "data" => json_decode($data_from_server),
                "status" => "success",
                "message" => "Request authorized"
            );  
    
        }catch (Exception $e){
    
            http_response_code(401);
    
            $data_insert=array(
                //"data" => $data_from_server,
                "jwt" => $jwt,
                "status" => "error",
                "message" => $e->getMessage()
            );
    
        }   
    }
    
    echo json_encode($data_insert);
    ?>
    

提交回复
热议问题