angularmoodlemoodle-apimoodle-mobile

Login API in moodle angular 8


I need to login my user from my website to the moodle page.I have build function that create user and make enroll to course,Now i need to make auto login.

I have set the "moodle_mobile_app" that have return my token of the user ,but i can find any service that make the login.

I have all at my side UserName,Password,Token i just need some web service from moodle that can receive this data and return login Url

"/login/token.php?username=YOUR_FORM_USERNAME&password=YOUR_FORM_PASSWORD&service=moodle_mobile_app"

Do any one know the trick/way how can i make user be logged-in to mooble from my website?


Solution

  • I have found this plugin that do the work

    https://moodle.org/plugins/auth_userkey
    
    https://github.com/catalyst/moodle-auth_userkey/blob/MOODLE_33PLUS/README.md
    

    Download,and install it

    Home -> Site administration -> Plugins -> Install plugins

    Just drag your zip file with plugin that you have downloaded Plugin install

    Set your plugin steps: your have full description here

    > 1.Install the plugin as usual.
    > 2.Enable and configure just installed plugin. Set required Mapping field, User key life time, IP restriction and Logout redirect URL.
    > 3.Enable web service advance feature (Admin > Advanced features), more info http://docs.moodle.org/en/Web_services
    > 4.Enable one of the supported protocols (Admin > Plugins > Web services > Manage protocols)
    > 5.Create a token for a specific user and for the service 'User key authentication web service' (Admin > Plugins > Web services > Manage
    > tokens)
    > 6.Make sure that the "web service" user has 'auth/userkey:generatekey' capability.
    > 7.Configure your external application to make a web call to get login URL.
    > 8.Redirect your users to this URL to be logged in to Moodle.
    

    Then download sample-ws-clients

    Unzip and ,add it to your root folder,then I have add file named sso.php to folder Online.mydomain.com\sample-ws-clients-master\PHP-REST\sso.php

    My sso.php code that reciving email,and returm login url back to you

    <?php
    
    /**
     * @param   string $useremail Email address of user to create token for.
     * @param   string $firstname First name of user (used to update/create user).
     * @param   string $lastname Last name of user (used to update/create user).
     * @param   string $username Username of user (used to update/create user).
     * @param   string $ipaddress IP address of end user that login request will come from (probably $_SERVER['REMOTE_ADDR']).
     * @param int      $courseid Course id to send logged in users to, defaults to site home.
     * @param int      $modname Name of course module to send users to, defaults to none.
     * @param int      $activityid cmid to send logged in users to, defaults to site home.
     * @return bool|string
     */
    
     $userEmail = $_GET["email"];
     $courseid = $_GET["id"];
    
    
    function getloginurl($useremail, $courseid) {
        require_once('./curl.php');
    
    
        $token        = 'your token';
        $domainname   = 'your domain';
        $functionname = 'auth_userkey_request_login_url';
    
    
        $param = [
            'user' => [
                'username'  => $useremail
            ] 
        ]; 
    
        $serverurl = $domainname . '/webservice/rest/server.php' . '?wstoken=' . $token . '&wsfunction=' . $functionname . '&moodlewsrestformat=json';
        $curl = new curl; // The required library curl can be obtained from https://github.com/moodlehq/sample-ws-clients 
    
        try {
            $resp     = $curl->post($serverurl, $param);
            $resp     = json_decode($resp);
    
    
            if ($resp && !empty($resp->loginurl)) {
                $loginurl = $resp->loginurl; 
    
    
            }
        } catch (Exception $ex) {
            return false;
        }
    
        if (!isset($loginurl)) {
    
            return false;
        }
    
        $path = '';
        if (isset($courseid)) {
    
            $path = '&wantsurl=' . $domainname . '/course/view.php?id=' . $courseid;
        }
        if (isset($modname) && isset($activityid)) {
            $path = '&wantsurl=' . urlencode("$domainname/mod/$modname/view.php?id=$activityid");
        }
    
    
        echo $loginurl . $path;
        header('Location:' . $loginurl . $path);
        return $loginurl . $path;
    }
    
    getloginurl($userEmail, $courseid);