source: extensions/oAuth/include/test/examples/signin_signup/application/controllers/authentications.php @ 20293

Last change on this file since 20293 was 20293, checked in by mistic100, 11 years ago

first commit of oAuth plugin, still in developpement

File size: 4.2 KB
Line 
1<?php
2class authentications extends controller {
3        function authenticatewith( $provider )
4        {
5                // set on application.config.php
6                GLOBAL $hybridauth_config;
7
8                try{
9                // create an instance for Hybridauth with the configuration file path as parameter
10                        $hybridauth = new Hybrid_Auth( $hybridauth_config );
11
12                // try to authenticate the selected $provider
13                        $adapter = $hybridauth->authenticate( $provider );
14
15                // grab the user profile
16                        $user_profile = $adapter->getUserProfile();
17
18                // load user and authentication models, we will need them...
19                        $authentication = $this->loadModel( "authentication" );
20                        $user = $this->loadModel( "user" );
21
22                # 1 - check if user already have authenticated using this provider before
23                        $authentication_info = $authentication->find_by_provider_uid( $provider, $user_profile->identifier );
24
25                # 2 - if authentication exists in the database, then we set the user as connected and redirect him to his profile page
26                        if( $authentication_info ){
27                                // 2.1 - store user_id in session
28                                $_SESSION["user"] = $authentication_info["user_id"]; 
29
30                                // 2.2 - redirect to user/profile
31                                $this->redirect( "users/profile" );
32                        }
33
34                # 3 - else, here lets check if the user email we got from the provider already exists in our database ( for this example the email is UNIQUE for each user )
35                        // if authentication does not exist, but the email address returned  by the provider does exist in database,
36                        // then we tell the user that the email  is already in use
37                        // but, its up to you if you want to associate the authentication with the user having the adresse email in the database
38                        if( $user_profile->email ){
39                                $user_info = $user->find_by_email( $user_profile->email );
40
41                                if( $user_info ) {
42                                        die( '<br /><b style="color:red">Well! the email returned by the provider ('. $user_profile->email .') already exist in our database, so in this case you might use the <a href="index.php?route=users/login">Sign-in</a> to login using your email and password.</b>' );
43                                }
44                        }
45
46                # 4 - if authentication does not exist and email is not in use, then we create a new user
47                        $provider_uid  = $user_profile->identifier;
48                        $email         = $user_profile->email;
49                        $first_name    = $user_profile->firstName;
50                        $last_name     = $user_profile->lastName;
51                        $display_name  = $user_profile->displayName;
52                        $website_url   = $user_profile->webSiteURL;
53                        $profile_url   = $user_profile->profileURL;
54                        $password      = rand( ) ; # for the password we generate something random
55
56                        // 4.1 - create new user
57                        $new_user_id = $user->create( $email, $password, $first_name, $last_name ); 
58
59                        // 4.2 - creat a new authentication for him
60                        $authentication->create( $new_user_id, $provider, $provider_uid, $email, $display_name, $first_name, $last_name, $profile_url, $website_url );
61 
62                        // 4.3 - store the new user_id in session
63                        $_SESSION["user"] = $new_user_id;
64
65                        // 4.4 - redirect to user/profile
66                        $this->redirect( "users/profile" );
67                }
68                catch( Exception $e ){
69                        // Display the recived error
70                        switch( $e->getCode() ){ 
71                                case 0 : $error = "Unspecified error."; break;
72                                case 1 : $error = "Hybriauth configuration error."; break;
73                                case 2 : $error = "Provider not properly configured."; break;
74                                case 3 : $error = "Unknown or disabled provider."; break;
75                                case 4 : $error = "Missing provider application credentials."; break;
76                                case 5 : $error = "Authentication failed. The user has canceled the authentication or the provider refused the connection."; break;
77                                case 6 : $error = "User profile request failed. Most likely the user is not connected to the provider and he should to authenticate again."; 
78                                             $adapter->logout(); 
79                                             break;
80                                case 7 : $error = "User not connected to the provider."; 
81                                             $adapter->logout(); 
82                                             break;
83                        } 
84
85                        // well, basically your should not display this to the end user, just give him a hint and move on..
86                        $error .= "<br /><br /><b>Original error message:</b> " . $e->getMessage(); 
87                        $error .= "<hr /><pre>Trace:<br />" . $e->getTraceAsString() . "</pre>"; 
88
89                        // load error view
90                        $data = array( "error" => $error ); 
91                        $this->loadView( "pages/error", $data );
92                }
93        }
94}
Note: See TracBrowser for help on using the repository browser.