· 9 years ago · Sep 19, 2016, 06:12 PM
1$scope.login = function (userLogin) {
2
3 var loginOptions = {'inAppBrowserOptions': {'hidden': true}};
4
5 $ionicAuth.login('custom', userLogin, loginOptions).then(function (data) {
6 Alert.showAlert('Success',JSON.stringify(data));
7 },function(err){
8 Alert.showAlert('Failed:',JSON.stringify(err));
9 });
10}
11
12<?php
13// custom authentication for Ionic Apps
14/**
15 * @param string GET parameter token.
16 * @param string GET parameter state.
17 * @param string GET parameter redirect uri.
18 * @return string Redirect URI.
19 * @throws Exception
20 */
21
22require_once('../vendor/autoload.php');
23
24use FirebaseJWTExpiredException;
25use FirebaseJWTJWT;
26
27include($_SERVER['DOCUMENT_ROOT']."/connect/config.php");
28
29
30try {
31 if (isset($_GET['token']) && isset($_GET['state']) && isset($_GET['redirect_uri'])) {
32
33 $token = $_GET['token'];
34 $state = $_GET['state'];
35 $redirect_uri = $_GET['redirect_uri'];
36
37 $decoded = JWT::decode($token, SECRET_KEY, array('HS256'));
38
39 $email = $decoded->data->email;
40 $password = $decoded->data->password;
41
42 $results = mysqli_query($dbc, "SELECT userID, fname, lname, userName, password, active FROM v_311users WHERE email='".$email."' LIMIT 1");
43 $res_match = mysqli_num_rows($results);
44 $res = mysqli_fetch_assoc($results);
45
46 if ($res_match == 1){
47
48 $userID = $res['userID'];
49 $active = $res['active'];
50 $pw = $res['password'];
51 $fname = $res['fname'];
52 $lname = $res['lname'];
53
54
55 if (password_verify($password, $pw)) {
56
57 if($active == 1){
58
59 $custom->name = $fname.' '.$lname;
60 $custom->email = $email;
61 $payload = ['user_id' => $userID, 'custom' => $custom];
62
63 $token = JWT::encode($payload, SECRET_KEY);
64
65 $url = $redirect_uri . '&' . http_build_query([
66 'token' => $token,
67 'state' => $state,
68 # TODO: Take out the redirect_uri parameter before production
69 //'redirect_uri' => 'https://api.ionic.io/auth/integrations/custom/success',
70 ]);
71
72 header('Location: '.$url);
73 exit();
74 } else {
75 throw new Exception('Account Not Activated', 40);
76 }
77 } else {
78 throw new Exception('Invalid Credentials', 30);
79 }
80 } else {
81 throw new Exception('Account Not Found', 20);
82 }
83
84
85 } else {
86 // something failed with POST, should never get here!
87 throw new Exception('Missing Parameters', 10);
88 }
89
90} catch (Exception $e) {
91 header("HTTP/1.1 401 Unauthorized");
92 echo json_encode(['error' => $e->getMessage(), 'code' => $e->getCode()]);
93 exit();
94}
95
96?>