1

I'm currently working on a Ionic app with a login validation feature. The objective is to verify user-entered login details by cross-referencing them with the corresponding username and password stored in the database. However, I'm faced with the challenge of integrating Moodle for authentication. I have a Moodle site established, and a database is already in place, but I'm uncertain about the most effective approach to tackle this integration. Any guidance or insights would be greatly appreciated.

Here is the code I currently have for the login validation:

import { Component } from '@angular/core';
import { NavController } from '@ionic/angular';

@Component({
  selector: 'app-home',
  templateUrl: 'home.page.html',
  styleUrls: ['home.page.scss'],
})
export class HomePage {
  username!: string;
  password!: string;

  constructor(private navCtrl: NavController) {}

  login() {

    if (this.username === '123' && this.password === '123') {
      // Navigate to another page after successful login
      this.navCtrl.navigateForward('/home/report-page'); 
    } else {
      // Show an error message for unsuccessful login
      console.log('Invalid username or password');
    }
  }
}

Update on a new attempt:

login() {
    const moodleEndpoint = 'https://mysite/webservice/rest/server.php';
    const webServiceToken = 'your_web_service_token';

    const params = {
      wstoken: webServiceToken,
      wsfunction: 'core_user_login',
      moodlewsrestformat: 'json',
      username: this.username,
      password: this.password,
    };

    this.http.get(moodleEndpoint, { params }).subscribe(
      (response: any) => {
        if (response.errorcode) {
          console.log('Login failed. Error: ' + response.errorcode);
        } else {
          // Successful login
          console.log('Login successful');
          // You can navigate to another page or perform other actions here
          this.navCtrl.navigateForward('/home/report-page');
        }
      },
      (error) => {
        console.error('Login failed. Error: ', error);
      }
    );
  }
}

I'm not too sure what mooodle webservice I would use tho.

1

1 Answer 1

0

I seem to have solved it. The login app sends a POST request to a server side API that accepts the user input and then the php code checks if the password is valid.

Here is the the php that receives the user input POST then gives the user input to the function that does the validation:

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $raw_data = file_get_contents('php://input'); // Get raw data from request body

    // Check if data is in JSON format (optional)
    if (strpos($raw_data, '{') !== false && strpos($raw_data, '}') !== false) {
        $data = json_decode($raw_data, true); // Decode JSON data

        if (isset($data['username']) && isset($data['password'])) {
            // Access username and password from decoded data
            $username = $data['username'];
            $password = $data['password'];

here is the verification logic used:

function user_login($username, $password)
{
    global $CFG, $DB, $USER;

    if (!$user = $DB->get_record('user', array('username' => $username))) {
        return false; // User not found
    }

    // Verify the password using password_verify() instead of directly comparing hashes
    if (password_verify($password, $user->password)) {
        return true; // Passwords match
    } else {
        return false; // Passwords do not match
    }
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.