Example #1
0
<?php

use Katanium\Models\User;
$app->get('/activate', function ($req, $res, $args) {
    // Get the email and identifier
    $params = $req->getParams();
    $email = $params['email'];
    $identifier = $params['identifier'];
    $hashedID = $this->get('hash')->hash($identifier);
    $user = User::where('email', $email)->where('active', 0)->first();
    if (!$user || !$this->get('hash')->hashCheck($user->active_hash, $hashedID)) {
        $app->flash('msg', 'Maaf, terdapat kesalahan dalam pengaktifan akun Anda di Katanium');
    } else {
        $user->activateAccount();
        $app->flash('msg', 'Akun Anda telah aktif. Anda bisa login sekarang');
        return $res->withRedirect($this->router->path_for('dashboard'));
        // Redirect to user-profile
    }
})->setName('activate')->add($app->getContainer()['guest']);
Example #2
0
    $httpGet = $req->getQueryParams();
    $redirectUrl = count($httpGet) ? $httpGet['redirectUrl'] : $app->urlFor('home');
    $data = ['login' => true, 'redirectUrl' => $redirectUrl];
    return $this->view->render($res, 'auth/login.twig', $data);
})->setName('login')->add($app->getContainer()['guest']);
/**
 * Proccess login data
 */
$app->post('/login', function ($req, $res, $args) use($app) {
    $params = $req->getParams();
    $v = $this['validator'];
    $v->validate(['email' => [$params['email'], 'required|email'], 'password' => [$params['password'], 'required']]);
    $params['remember'] = isset($params['remember']) ? $params['remember'] : 'off';
    if ($v->passes()) {
        // Search in DB
        $user = User::where('email', $req->email)->active()->first();
        // If email and password both exist and match in database
        if ($user && $this['hash']->passwordCheck($req->password, $user->password)) {
            // Set session for login
            $_SESSION[$this['myConfig']->get('auth.session')] = $user->user_id;
            if ($req['remember'] === 'on') {
                $rememberIdentifier = $app->randomlib->generateString(128);
                $rememberToken = $app->randomlib->generateString(128);
                $user->updateRememberCredentials($rememberIdentifier, $this['hash']->hash($rememberToken));
                // Set the cookie
                $app->setCookie($this['myConfig']->get('auth.remember'), "{$rememberIdentifier}___{$rememberToken}", \Carbon\Carbon::parse('+1 week')->timestamp);
            }
            // Notify and rediect to where it should belong
            $redirectUrl = $params['redirectUrl'] !== $this->router->pathFor('login') ? $params['redirectUrl'] : $this->router->pathFor('home');
            return $res->withRedirect($redirectUrl);
        } else {
Example #3
0
     * display the desired user profile
     *
     * @param [string] displayname : sort of username, not fullname
     */
    $app->get('/:displayName', function ($displayName) use($app) {
        $user = User::where('displayName', $displayName)->first();
        if (!$user) {
            $app->notFound();
        }
        $app->view()->appendData(['user' => $user]);
        $app->render('user-profile.twig');
    })->name('user');
    /**
     * USER POSTS
     * get all posts authored by the user
     *
     * @param [string] displayname     : user's username
     * @param [int]    page (optional) : number used to paginate the posts
     * @return all paginated user posts in JSON
     */
    $app->get('/:displayName/posts(/:page)', function ($displayName, $page = 0) use($app) {
        $user = User::where('displayName', $displayName)->first(['user_id']);
        if (!$user) {
            $app->notFound();
        }
        $userPosts = (new Post())->searchPost('', $user->user_id);
        $userPosts->where('status', 'published');
        $app->response->headers->set('Content-Type', 'application/json');
        echo json_encode($userPosts->get());
    })->name('user.posts');
});
Example #4
0
 public function validate_uniqueEmail($value, $input, $args)
 {
     return !(bool) User::where('email', $value)->count();
 }
Example #5
0
/**
 * Proccess register data
 */
$app->post('/register', function ($req, $res, $args) use($app) {
    $params = $req->getParams();
    // Set validator
    $v = $this['validator'];
    $v->validate(['email' => [params['email'], 'required|email|uniqueEmail'], 'fullName' => [params['fullName'], 'required'], 'password' => [params['password'], 'required|min(6)'], 'password_confirm' => [params['password_confirm'], 'required|matches(password)']]);
    if ($v->passes()) {
        // Before anything else, generate a random string
        // with the purpose of activating the newly-registred user later
        $identifier = $this['randomlib']->generateString(128);
        // Create username
        // Take 'dewey992' out of dewey992@gmail.com
        $uname = explode('@', params['email']);
        $user = new User();
        // then check if the username already exists
        if ((bool) $user->where('displayName', $uname[0])->count()) {
            // explode once more
            // Take the 'gmail' from gmail.com
            $emailhost = explode('.', $uname[1]);
            // Concat the string
            // So that the result would be dewey992gmail
            $user->displayName = $uname[0] . $emailhost[0];
        } else {
            $user->displayName = $uname[0];
        }
        // Save to DB
        $user->email = $post->email;
        $user->fullName = $post->fullName;
        $user->password = $this['hash']->password($post->password);