/** * Store a newly created resource in storage. * * @return Response */ public function store(MecanexUserRequest $request) { // $thematic_area=$request->interest; // dd($thematic_area); $mecanexuser = MecanexUser::create($request->all()); $username = $request->username; $user = User::where('username', $username)->get()->first(); // create records in table users_terms-scores once a mecanex user has been created $terms = Term::all(); $total_terms = $terms->count(); foreach ($terms as $term) { $mecanexuser->term()->sync([$term->id => ['user_score' => 0]], false); $mecanexuser->profilescore()->sync([$term->id => ['profile_score' => 0]], false); } //create record in table mecanex_user_term_home_term_neighbor once a mecanex user has been created for ($i = 1; $i <= $total_terms; $i++) { for ($j = $i + 1; $j <= $total_terms; $j++) { $mec_matrix = new MecanexUserTermHomeTermNeighbour(); $mec_matrix->mecanex_user_id = $mecanexuser->id; $mec_matrix->term_home_id = $i; $mec_matrix->term_neighbor_id = $j; $mec_matrix->link_score = 0.1; $mec_matrix->save(); } } //the following is only needed for linking an existing authorized user (users_table) with a new mecanex user provided they have the same username if (empty($user)) { $response = array('message' => 'Store successful'); } else { $mecanexuser->user_id = $user->id; $mecanexuser->save(); $response = array('message' => 'Store successful'); } return response($response, 201)->header('Content-Type', 'application/json'); }
/** * Display a listing of the resource. * * @return Response */ public function login(Request $request, SammyK\LaravelFacebookSdk\LaravelFacebookSdk $fb) { //test if token for FB login is enough $token = $request->token; // return $token; // does not work since $token is string but should be token // if (! $token->isLongLived()) { // // OAuth 2.0 client handler // $oauth_client = $fb->getOAuth2Client(); // // // Extend the access token. // try { // $token = $oauth_client->getLongLivedAccessToken($token); // } catch (Facebook\Exceptions\FacebookSDKException $e) { // dd($e->getMessage()); // } // } //this is for not include $token in the get calls $fb->setDefaultAccessToken($token); // Get basic info on the user from Facebook. try { $response = $fb->get('/me?fields=id,email'); } catch (Facebook\Exceptions\FacebookSDKException $e) { dd($e->getMessage()); } try { $profileresponse = $fb->get('/me?fields=id,name,gender'); } catch (Facebook\Exceptions\FacebookSDKException $e) { dd($e->getMessage()); } // Convert the response to a `Facebook/GraphNodes/GraphUser` collection $facebook_user = $response->getGraphUser(); $mecanex_user = $profileresponse->getGraphUser(); $existing_mecanex_user = MecanexUser::where('email', '=', $facebook_user["email"])->get()->first(); $existing_user = User::where('email', '=', $facebook_user["email"])->get()->first(); // Create the user if it does not exist or update the existing entry. // This will only work if you've added the SyncableGraphNodeTrait to your User model. if ($existing_user == null) { $facebook_user["username"] = "******" . $facebook_user["id"]; $user = User::createOrUpdateGraphNode($facebook_user); } else { $facebook_user["username"] = $existing_user->username; $user = $existing_user; $user->facebook_user_id = $facebook_user["id"]; $user->save(); } //store profile data in mecanex_users table $id = $user->id; $facebook_id = $user->facebook_user_id; if ($existing_mecanex_user == null) { $username = "******" . $user->facebook_user_id; } else { $username = $existing_mecanex_user->username; } $email = $user->email; $fullname = $mecanex_user->getName(); $fullname = explode(" ", $fullname); $name = $fullname[0]; $surname = $fullname[1]; $gender = $mecanex_user->getGender(); if ($gender == 'female') { $gender_id = 2; } else { $gender_id = 1; } $fbuser = MecanexUser::firstOrNew(array('email' => $email)); $fbuser->username = $username; $fbuser->user_id = $id; $fbuser->facebook_user_id = $facebook_id; $fbuser->gender_id = $gender_id; $fbuser->name = $name; $fbuser->surname = $surname; $fbuser->email = $email; $fbuser->save(); // Log the user into Laravel Auth::login($user); // create records in table users_terms-scores once a mecanex user has been created if ($existing_mecanex_user == null) { $terms = Term::all(); $total_terms = count($terms); foreach ($terms as $term) { $fbuser->term()->sync([$term->id => ['user_score' => 0]], false); $fbuser->profilescore()->sync([$term->id => ['profile_score' => 0]], false); } for ($i = 1; $i <= $total_terms; $i++) { for ($j = $i + 1; $j <= $total_terms; $j++) { $mec_matrix = new MecanexUserTermHomeTermNeighbour(); $mec_matrix->mecanex_user_id = $fbuser->id; $mec_matrix->term_home_id = $i; $mec_matrix->term_neighbor_id = $j; $mec_matrix->link_score = 0.05; $mec_matrix->save(); } } } $response = ['username' => $username, 'message' => 'User was successfully logged in']; $statusCode = 201; return response($response, $statusCode)->header('Content-Type', 'application/json'); }
/** * Store a newly created resource in storage. * * @return Response */ public function store(CreateProfileRequest $request) { //validation first $request->all(); $username = Auth::user()->username; $name = Input::get('name'); $surname = Input::get('surname'); $gender_id = Input::get('gender_id'); $age_id = Input::get('age_id'); $country_id = Input::get('country_id'); $occupation_id = Input::get('occupation_id'); $education_id = Input::get('education_id'); //$facebook=Input::get('facebook'); //$twitter=Input::get('twitter'); $mecanex_user = new MecanexUser(['username' => $username, 'name' => $name, 'surname' => $surname, 'gender_id' => $gender_id, 'age_id' => $age_id, 'education_id' => $education_id, 'occupation_id' => $occupation_id, 'country_id' => $country_id]); $user = User::find(Auth::user()->id); $mecanex_user = $user->profile()->save($mecanex_user); //$mecanex_user->save(); // create records in table users_terms-scores once a mecanex user has been created $terms = Term::all(); $total_terms = $terms->count(); foreach ($terms as $term) { $mecanex_user->term()->sync([$term->id => ['user_score' => 0]], false); $mecanex_user->profilescore()->sync([$term->id => ['profile_score' => 0]], false); } //create record in table mecanex_user_term_home_term_neighbor once a mecanex user has been created for ($i = 1; $i <= $total_terms; $i++) { for ($j = $i + 1; $j <= $total_terms; $j++) { $mec_matrix = new MecanexUserTermHomeTermNeighbour(); $mec_matrix->mecanex_user_id = $mecanex_user->id; $mec_matrix->term_home_id = $i; $mec_matrix->term_neighbor_id = $j; $mec_matrix->link_score = 0.05; $mec_matrix->save(); } } return view('user.profile')->with('message', 'You can now state your interests'); // }
public function callback(SammyK\LaravelFacebookSdk\LaravelFacebookSdk $fb) { // $token = $fb->getAccessTokenFromRedirect(); // dd($token); // Obtain an access token. try { $token = $fb->getAccessTokenFromRedirect(); } catch (Facebook\Exceptions\FacebookSDKException $e) { dd($e->getMessage()); } // Access token will be null if the user denied the request // or if someone just hit this URL outside of the OAuth flow. if (!$token) { // Get the redirect helper $helper = $fb->getRedirectLoginHelper(); if (!$helper->getError()) { abort(403, 'Unauthorized action.'); } // User denied the request // echo '<p>Error: ' . $helper->getError(); // echo '<p>Code: ' . $helper->getErrorCode(); // echo '<p>Reason: ' . $helper->getErrorReason(); // echo '<p>Description: ' . $helper->getErrorDescription(); // exit ; dd($helper->getError(), $helper->getErrorCode(), $helper->getErrorReason(), $helper->getErrorDescription()); } $fb->setDefaultAccessToken($token); if (!$token->isLongLived()) { // OAuth 2.0 client handler $oauth_client = $fb->getOAuth2Client(); // Extend the access token. try { $token = $oauth_client->getLongLivedAccessToken($token); } catch (Facebook\Exceptions\FacebookSDKException $e) { dd($e->getMessage()); } } //this is for not include $token in the get calls $fb->setDefaultAccessToken($token); // Get basic info on the user from Facebook. try { $response = $fb->get('/me?fields=id,email'); } catch (Facebook\Exceptions\FacebookSDKException $e) { dd($e->getMessage()); } try { $profileresponse = $fb->get('/me?fields=id,name,gender'); } catch (Facebook\Exceptions\FacebookSDKException $e) { dd($e->getMessage()); } // Convert the response to a `Facebook/GraphNodes/GraphUser` collection $facebook_user = $response->getGraphUser(); $mecanex_user = $profileresponse->getGraphUser(); $existing_mecanex_user = MecanexUser::where('facebook_user_id', '=', $facebook_user["id"])->get()->first(); $existing_user = User::where('facebook_user_id', '=', $facebook_user["id"])->get()->first(); // Create the user if it does not exist or update the existing entry. // This will only work if you've added the SyncableGraphNodeTrait to your User model. if ($existing_user == null) { $facebook_user["username"] = "******" . $facebook_user["id"]; } else { $facebook_user["username"] = $existing_user->username; } $user = User::createOrUpdateGraphNode($facebook_user); //store profile data in mecanex_users table $id = $user->id; $facebook_id = $user->facebook_user_id; if ($existing_mecanex_user == null) { $username = "******" . $user->facebook_user_id; } else { $username = $existing_mecanex_user->username; } $email = $user->email; $fullname = $mecanex_user->getName(); $fullname = explode(" ", $fullname); $name = $fullname[0]; $surname = $fullname[1]; $gender = $mecanex_user->getGender(); if ($gender == 'female') { $gender_id = 2; } else { $gender_id = 1; } $fbuser = MecanexUser::firstOrNew(array('facebook_user_id' => $facebook_id)); $fbuser->username = $username; $fbuser->user_id = $id; $fbuser->facebook_user_id = $facebook_id; $fbuser->gender_id = $gender_id; $fbuser->name = $name; $fbuser->surname = $surname; $fbuser->email = $email; $fbuser->save(); // Log the user into Laravel Auth::login($user); if ($existing_mecanex_user == null) { // create records in table users_terms-scores once a mecanex user has been created $terms = Term::all(); $total_terms = count($terms); foreach ($terms as $term) { $fbuser->term()->sync([$term->id => ['user_score' => 0]], false); $fbuser->profilescore()->sync([$term->id => ['profile_score' => 0]], false); } for ($i = 1; $i <= $total_terms; $i++) { for ($j = $i + 1; $j <= $total_terms; $j++) { $mec_matrix = new MecanexUserTermHomeTermNeighbour(); $mec_matrix->mecanex_user_id = $fbuser->id; $mec_matrix->term_home_id = $i; $mec_matrix->term_neighbor_id = $j; $mec_matrix->link_score = 0.05; $mec_matrix->save(); } } } return redirect('/home')->with('message', 'Successfully logged in with Facebook'); }