This commit is contained in:
Kekskurse 2019-04-28 17:55:38 +02:00
parent aa306ff4b1
commit 78e7b0b8d2
3 changed files with 28 additions and 8 deletions

View file

@ -72,13 +72,29 @@ class UserController extends BaseController
//If Recptache is enabled check it at the beginning //If Recptache is enabled check it at the beginning
if(Setting::getSettingValue("recaptcha_v2_register")) { if(Setting::getSettingValue("recaptcha_v2_register")) {
$reCaptcha = new ReCaptcha(Setting::getSettingValue("recaptcha_v2_secret")); $reCaptcha = new ReCaptcha(Setting::getSettingValue("recaptcha_v2_secret"));
$response = $reCaptcha->verify($request->input("g-recaptcha-response")); $captchaResponse = $reCaptcha->verify($request->input("g-recaptcha-response"));
if(!$response->isSuccess()) { if(!$captchaResponse->isSuccess()) {
throw new HTTPException(400, "Captcha validation failed"); throw new HTTPException(400, "Captcha validation failed");
} }
} }
$invite = Invite::query()->where("code", "=", $request->input("invite"))->first();
if($invite != null) {
if($invite->status != "active") {
throw new HTTPException("Invite code invalide");
}
if(!empty($invite->username) && $request->input("username") != $invite->username) {
throw new HTTPException("Invalide username for invite");
}
} else {
$setting = Setting::query()->where("name", "=", "registration_possible")->firstOrFail();
if(!$setting->value) {
throw new HTTPException("400", "Registration disabled");
}
}
$this->validate($request, [ $this->validate($request, [
'username' => 'required|max:255|min:5|regex:@^[a-z0-9]*$@|unique:users', 'username' => 'required|max:255|min:5|regex:@^[a-z0-9]*$@|unique:users',
'password' => 'required|min:8', 'password' => 'required|min:8',

View file

@ -1,4 +1,5 @@
<?php include(__DIR__."/../layout/top.php"); ?> <?php include(__DIR__."/../layout/top.php"); ?>
<script src="https://www.google.com/recaptcha/api.js?onload=onloadCallback&render=explicit" async defer></script><br>
<div class="row"> <div class="row">
<div class="col-md-12"> <div class="col-md-12">
<h3>Register</h3> <h3>Register</h3>
@ -27,6 +28,7 @@
<input name="password" type="password" placeholder="Password" class="form-control"> <input name="password" type="password" placeholder="Password" class="form-control">
<b>E-Mail</b> <span id="msg_mail" class="mail"></span> <b>E-Mail</b> <span id="msg_mail" class="mail"></span>
<input type="email" placeholder="E-Mail" name="mail" class="form-control"> <input type="email" placeholder="E-Mail" name="mail" class="form-control">
<div id="captcha" style="padding-top: 10px;"></div>
<input type="submit" class="btn btn-success" value="Register" style="margin-top: 10px;"> <input type="submit" class="btn btn-success" value="Register" style="margin-top: 10px;">
</form> </form>
</div> </div>
@ -39,7 +41,7 @@
url: "/api/v1/user/captcha", url: "/api/v1/user/captcha",
success: function (res) { success: function (res) {
captchaConfig = res.data; captchaConfig = res.data;
if(captchaConfig["login"]) { if(captchaConfig["register"]) {
grecaptcha.render('captcha', { grecaptcha.render('captcha', {
'sitekey' : captchaConfig["key"] 'sitekey' : captchaConfig["key"]
}); });
@ -50,12 +52,13 @@
} }
$(document).ready(function () { $(document).ready(function () {
console.log("READY"); console.log("READY");
getCaptchaConfig();
$("#register").submit(function (e) { $("#register").submit(function (e) {
e.preventDefault(); e.preventDefault();
var form = $(this); var form = $(this);
$.ajax({ $.ajax({
type: "POST", type: "POST",
url: "/gui/register", url: "/api/v1/user/register",
data: form.serialize(), // serializes the form's elements. data: form.serialize(), // serializes the form's elements.
success: function (data) { success: function (data) {
window.location.href = "/gui/login"; window.location.href = "/gui/login";
@ -66,13 +69,13 @@
$(e).html(""); $(e).html("");
}) })
if(data.status == 422) { if(data.status == 422) {
$.each(data.responseJSON, function( key, value ) { $.each(data.responseJSON.data, function( key, value ) {
$("#msg_"+key).html(value[0]); $("#msg_"+key).html(value[0]);
}); });
} else {
swal(data.responseJSON.msg, '', "error")
} }
if(data.status == 401) { grecaptcha.reset();
alert("Usernamme/Password falsch");
}
} }
}); });
}); });

View file

@ -49,6 +49,7 @@ $router->group(['prefix' => 'api', 'middleware' => 'auth'], function () use ($ro
}); });
}); });
$router->post("api/v1/user/login", ['uses' => 'API\UserController@passwordLogin']); $router->post("api/v1/user/login", ['uses' => 'API\UserController@passwordLogin']);
$router->post("api/v1/user/register", ['uses' => 'API\UserController@register']);
$router->get("api/v1/user/captcha", ['uses' => 'API\UserController@reCAPTCHA']); $router->get("api/v1/user/captcha", ['uses' => 'API\UserController@reCAPTCHA']);
$router->get("api/v1/user/invites", ['uses' => 'API\UserController@getInviteCodeInfo']); $router->get("api/v1/user/invites", ['uses' => 'API\UserController@getInviteCodeInfo']);