Quantcast
Channel: Ionic Framework - Ionic Forum
Viewing all articles
Browse latest Browse all 49177

Ionic Custom Auth not working on fail

$
0
0

@rgecy wrote:

I have a custom php script I wrote for authenticating users using the ionic custom auth. I am trying to get a response back with the inAppBrowser hidden so I am not using a web form. I can successfully login if the user credentials are correct, but if the user enters invalid credentials or there is an exception in the script, the ionic auth fail function is not called.

$scope.login = function (userLogin) {
        var loginOptions = {'inAppBrowserOptions': {'hidden': true}};
        $ionicAuth.login('custom', userLogin, loginOptions).then(function (data) {
            Alert.showAlert('Success',JSON.stringify(data));
        },function(err){
            Alert.showAlert('Failed:',JSON.stringify(err));
        });
}

PHP Auth Script

<?php
// custom authentication for Ionic Apps
/**
 * @param string GET parameter token.
 * @param string GET parameter state.
 * @param string GET parameter redirect uri.
 * @return string Redirect URI.
 * @throws Exception
 */

require_once('../vendor/autoload.php');

use \Firebase\JWT\ExpiredException;
use \Firebase\JWT\JWT;

include($_SERVER['DOCUMENT_ROOT']."/connect/config.php");

try {
	if (isset($_GET['token']) && isset($_GET['state']) && isset($_GET['redirect_uri'])) {

		$token 			= $_GET['token'];
		$state 			= $_GET['state'];
		$redirect_uri 	= $_GET['redirect_uri'];

		$decoded 	= JWT::decode($token, SECRET_KEY, array('HS256'));

		$email 		= $decoded->data->email;
		$password 	= $decoded->data->password;

		$results 	= mysqli_query($dbc, "SELECT userID, fname, lname, userName, password, active FROM v_311users WHERE email='".$email."' LIMIT 1");
		$res_match  = mysqli_num_rows($results);
		$res 		= mysqli_fetch_assoc($results);

		if ($res_match == 1){

			$userID		= $res['userID'];
			$active		= $res['active'];
			$pw 		= $res['password'];
			$fname 		= $res['fname'];
			$lname 		= $res['lname'];

			if (password_verify($password, $pw)) {

				if($active == 1){

					$custom->name = $fname.' '.$lname;
					$custom->email = $email;
                    $payload = ['user_id'   => $userID,  'custom' => $custom];

					$token = JWT::encode($payload, SECRET_KEY);

					$url = $redirect_uri . '&' . http_build_query([
            			'token' => $token,
            			'state' => $state,
            			# TODO: Take out the redirect_uri parameter before production
            			//'redirect_uri' => 'https://api.ionic.io/auth/integrations/custom/success',
        			]);

					header('Location: '.$url);
					exit();
				} else {
					throw new Exception('Account Not Activated', 40);
				}
			} else {
				throw new Exception('Invalid Credentials', 30);
			}
		} else {
			throw new Exception('Account Not Found', 20);
		}
	} else {
		// something failed with POST, should never get here!
		 throw new Exception('Missing Parameters', 10);
	}

} catch (Exception $e) {
	header('Content-type: application/json');
	header("HTTP/1.1 401 Unauthorized");
	echo json_encode(['error' => $e->getMessage(), 'code' => $e->getCode()]);
}

?>

Thanks for any help.

RGecy

Posts: 1

Participants: 1

Read full topic


Viewing all articles
Browse latest Browse all 49177

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>