silex-oauth icon indicating copy to clipboard operation
silex-oauth copied to clipboard

Question: how persist in database?

Open FrnandMG opened this issue 8 years ago • 1 comments

It is possible to persist user data, (user id, email, name) in database maybe mysql or sqlite, could you give me an example or link that could help me. I was searching a lot without any result.

Thanks in advance.

FrnandMG avatar Nov 21 '16 01:11 FrnandMG

Ok, I don't know if it's the right way but this is my solution,

Using Doctrine ORM Service Provider

$app->before(function (Symfony\Component\HttpFoundation\Request $request) use ($app) {
    $token = $app['security']->getToken();
    $app['user'] = null;

    if ($token && !$app['security.trust_resolver']->isAnonymous($token)) {
        $app['user'] = $token->getUser();
        $userData = $token->getUser();
        $userId = $userData->getUid();
        $em = $app['orm.em'];
        $query = $em->createQuery('SELECT u FROM App\User\Model\FBUser u WHERE u.uid = '.$userId);
        $registereduser = $query->getResult();
        if (!$registereduser) {
            $em->persist($userData);
            $em->flush();
        }
        
    }
});

If someone has a better way or the correct method, just comment.

FrnandMG avatar Nov 22 '16 19:11 FrnandMG