diff --git a/README.md b/README.md index 0ead193..26b0cc1 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,6 @@ Facebook and Twitter are currently supported. This spark is a work in progress. Things to be added: - Lots of refactoring -- Responses in lang file (sounds like an easy pull request to me!) - More Oauth providers ## Installing @@ -26,6 +25,8 @@ You can then load the spark with this: $this->load->spark('codeigniter-oauth/'); ``` +In the package there is a sample controller and view for reference. Remember to add your details to the config file, as well as setting your success callback URL in the Twitter/Facebook admin interface. + ## Usage Example ```php @@ -53,4 +54,4 @@ else var_dump($result); } } -``` \ No newline at end of file +``` diff --git a/controllers/signin.php b/controllers/signin.php new file mode 100644 index 0000000..b8956fe --- /dev/null +++ b/controllers/signin.php @@ -0,0 +1,69 @@ +load->spark('codeigniter-oauth-0.0.2'); + } + + public function index() { + $this->data['username'] = $this->input->cookie('username', TRUE); + if (empty($this->data['username']) && isset($_SERVER['HTTP_REFERER'])) { + $this->session->set_userdata('referrer', $_SERVER['HTTP_REFERER']); + } + $this->load->view('signin/index', $this->data); + } + + public function login($type = '') { + if (!empty($type)) { + if (isset($_GET['oauth_token']) && isset($_GET['oauth_verifier'])) { + $result = $this->oauth->authorize_result($type); + if($result->status === 'success') { + (isset($result->token2)) + ? $get_auth = $this->oauth->access($type, $result->token, $result->token2) + : $get_auth = $this->oauth->access($type, $result->token); + + // Set a cookie to remember the login status + $cookie = array( + 'name' => 'username', + 'value' => $get_auth->user['username'], + 'expire' => '86400' // 24 hours + ); + $this->input->set_cookie($cookie); + + $referrer = $this->session->userdata('referrer'); + if (!empty($referrer)) { + redirect($referrer, 'refresh'); + } else { + redirect('./signin', 'refresh'); + } + } else { + echo('Authentication error
'); + var_dump($result); + } + } else if (isset($_GET['denied'])) { + echo('Access denied'); + } else { + $this->oauth->authorize($type); + } + } else { + redirect('./signin', 'refresh'); + } + } + + public function logout() { + $username = $this->input->cookie('username', TRUE); + if ($username) { + // Clear the cookie + $cookie = array( + 'name' => 'username', + 'value' => $username, + 'expire' => '' + ); + $this->input->set_cookie($cookie); + } + redirect(base_url(), 'refresh'); + } +} +?> diff --git a/views/signin/index.php b/views/signin/index.php new file mode 100644 index 0000000..073cd48 --- /dev/null +++ b/views/signin/index.php @@ -0,0 +1,180 @@ + + +Logged in as '.$username.'

+

Logout

'); +} else { + echo('

Not logged in.

+

Login with Twitter

+

Login with Facebook

'); +} +?>