Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 2ecc462c95
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 121 lines (99 sloc) 3.076 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
<?php

namespace NinjAuth;

use Arr;
use Config;
use Input;
use Response;
use Session;

/**
* NinjAuth Controller
*
* @package FuelPHP/NinjAuth
* @category Controller
* @author Phil Sturgeon
* @copyright (c) 2012 HappyNinjas Ltd
* @license http://philsturgeon.co.uk/code/dbad-license
*/

class Controller extends \Controller
{
public static $linked_redirect = '/auth/linked';
public static $login_redirect = '/';
public static $register_redirect = '/auth/register';
public static $registered_redirect = '/';

public function before()
{
parent::before();

// Load the configuration for this provider
Config::load('ninjauth', true);
}

public function action_session($provider)
{
$url = Strategy::forge($provider)->authenticate();

Response::redirect($url);
}

public function action_callback($provider)
{
// Whatever happens, we're sending somebody somewhere
$status = Strategy::forge($provider)->login_or_register();

// Stuff should go with each type of response
switch ($status)
{
case 'linked':
$message = 'You have linked '.$provider.' to your account.';
$url = static::$linked_redirect;
break;

case 'logged_in':
$message = 'You have logged in.';
$url = static::$login_redirect;
break;

case 'registered':
$message = 'You have logged in with your new account.';
$url = static::$registered_redirect;
break;

case 'register':
$message = 'Please fill in any missing details and add a password.';
$url = static::$register_redirect;
break;

default:
throw new Exception('Strategy::login_or_register() has come up with a result that we dont know how to handle.');
}

Response::redirect($url);
}

public function action_register()
{
$user_hash = Session::get('ninjauth.user');
$authentication = Session::get('ninjauth.authentication');

// Working with what?
$strategy = Strategy::forge($authentication['provider']);

$full_name = Input::post('full_name') ?: Arr::get($user_hash, 'name');
$username = Input::post('username') ?: Arr::get($user_hash, 'nickname');
$email = Input::post('email') ?: Arr::get($user_hash, 'email');
$password = Input::post('password');

if ($username and $full_name and $email and $password)
{
$user_id = $strategy->adapter->create_user(array(
'username' => $username,
'email' => $email,
'full_name' => $full_name,
'password' => $password,
));

if ($user_id)
{
Model_Authentication::forge(array(
'user_id' => $user_id,
'provider' => $authentication['provider'],
'uid' => $authentication['uid'],
'access_token' => $authentication['access_token'],
'secret' => $authentication['secret'],
'refresh_token' => $authentication['refresh_token'],
'expires' => $authentication['expires'],
'created_at' => time(),
))->save();

Response::redirect(static::$registered_redirect);
}
}

$this->response->body = \View::forge('register', array(
'user' => (object) compact('username', 'full_name', 'email', 'password')
));
}
}
Something went wrong with that request. Please try again.