I want to modify Flarum's default login logic. I am developing a forum for my university and now need to directly modify Flarum's native login logic to authenticate users via my university's Eduroam accounts. This is the code I have so far. How should I override the native login logic?
public static function handleEduroamLogin($studentNumber, $password)
{
$emailForAuthentication = $studentNumber . '@sit.edu.cn';
$emailForDatabase = $studentNumber . '@mail.sit.edu.cn';
// To perform remote authentication.
$response = Http::asForm()->post('https://eduroam.ustc.edu.cn/cgi-bin/eduroam-test.cgi', [
'login' => $emailForAuthentication,
'password' => $password,
]);
if (strpos($response->body(), 'EAP Failure') !== false) {
throw new NotAuthenticatedException('Username or password incorrect, please try again.');
} elseif (strpos($response->body(), 'illegal') !== false) {
throw new DomainException('
Login failed, reason: Unauthorized operation, please contact the administrator.');
} elseif (strpos($response->body(), 'success') !== false) {
// Success,check user
$user = static::where('email', $emailForDatabase)->first();
if (!$user) {
// Create new user
$user = static::register($studentNumber, $emailForDatabase, $password);
$user->save();
}
return $user;
} else {
throw new NotAuthenticatedException('Error');
}