Social login hybridauth and parse php

Wednesday, November 4, 2015

I need to help for social login and parse.



I use hybridauth class for social logins (http://hybridauth.sourceforge.net/)



I successfully login and signup with this. But i cant define parseuser with login. Because i cant know user pass and i cant retrive pass.



How can i login without user password in php?



http://hybridauth.sourceforge.net/userguide/HybridAuth_Sessions.html
HybridAuth have sesssions and parse php sdk have



$user = ParseUser::become("session-token-here");



i think i need to use this. But i cant figure out. Anyone here use social login with parse?



Here is my code:



    <?php
require "parse.php";
use Parse\ParseUser;
use Parse\ParseException;

if( isset( $_REQUEST["provider"] ) )
{
// the selected provider
$provider_name = $_REQUEST["provider"];

try
{
// inlcude HybridAuth library
// change the following paths if necessary
$config = 'hybridauth/config.php';
require_once( "hybridauth/Hybrid/Auth.php" );

// initialize Hybrid_Auth class with the config file
$hybridauth = new Hybrid_Auth( $config );

// try to authenticate with the selected provider
$adapter = $hybridauth->authenticate( $provider_name );

// then grab the user profile
$user_profile = $adapter->getUserProfile();
}

// something went wrong?
catch( Exception $e )
{
print_r($e);
}

// check if the current user already have authenticated using this provider before
$user_exist = get_user_by_provider_and_id( $provider_name, $user_profile->identifier );

// if the used didn't authenticate using the selected provider before
// we create a new entry on database.users for him
if( ! $user_exist )
{
create_new_hybridauth_user(
$user_profile->email,
$user_profile->firstName,
$user_profile->lastName,
$provider_name,
$user_profile->identifier,
$user_profile->displayName,
$user_profile->description,
$user_profile->photoURL
);
}

// set the user as connected and redirect him
$_SESSION["user_connected"] = true;

header("Location: http://example.com");
}
function get_user_by_provider_and_id( $provider_name, $provider_user_id )
{
$query = ParseUser::query();
$query->equalTo("hybridauth_provider_name", $provider_name);
$query->equalTo("hybridauth_provider_uid", $provider_user_id);
$result = $query->find();
return $result;
}

/*
* get the user data from database by provider name and provider user id
**/
function create_new_hybridauth_user( $email, $first_name, $last_name, $provider_name, $provider_user_id, $display_name, $desc, $photourl )
{
// let generate a random password for the user
$password = md5( str_shuffle( "0123456789abcdefghijklmnoABCDEFGHIJ" ) );
$user = new ParseUser();
$user->set("username", $display_name);
$user->set("password", $password);
$user->set("email", $email);
$user->set("first_name", $first_name);
$user->set("last_name", $last_name);
$user->set("hybridauth_provider_name", $provider_name);
$user->set("hybridauth_provider_uid", $provider_user_id);
$user->set("description", $desc);
$user->set("photourl", $photourl);

try {
$user->signUp();
$currentUser = ParseUser::getCurrentUser();
} catch (ParseException $ex) {
// Show the error message somewhere and let the user try again.
echo "Error: " . $ex->getCode() . " " . $ex->getMessage();
}
}

0 comments:

Post a Comment