Example #1
0
    $app->response()->header('Content-Type', 'application/json');
    $req_body = json_decode($app->request->getBody());
    // Check if all fields are present
    if ($req_body->gender == NULL || $req_body->nickname == NULL || $req_body->password1 == NULL || $req_body->password2 == NULL || $req_body->school == NULL) {
        $app->halt(400, '{"message": "er is iets fout gegeaan"}');
    }
    // Check if username exists
    if (User::where('nickname', 'like', $req_body->nickname)->count()) {
        $app->halt(400, 'nickname_exists');
    }
    try {
        $user = new User();
        $user->nickname = $req_body->nickname;
        $user->gender = $req_body->gender;
        $user->password = sha1($req_body->password1);
        $user->educationLevel()->associate(EducationLevel::find((int) $req_body->schoolAdvice));
        $user->school()->associate(School::find((int) $req_body->school));
        $user->save();
        // Create token
        $token = new Token();
        $token->generateToken();
        $token->user()->associate($user);
        $token->save();
    } catch (Exception $e) {
        $app->halt(500, 'something_went_wrong');
    }
    echo $token->toJson();
});
$app->get('/user', function () use($app) {
    $app->response()->header('Content-Type', 'application/json');
    $token_key = $app->request->headers->get('Authorization');