public function run() { DB::table('users')->delete(); $user = new User(); $user->email = '*****@*****.**'; $user->password = Hash::make('adminadmin'); $user->username = '******'; $user->admin = TRUE; $user->active = TRUE; $user->save(); $user = new User(); $user->email = '*****@*****.**'; $user->password = Hash::make('editoreditor'); $user->username = '******'; $user->admin = FALSE; $user->editor = TRUE; $user->active = TRUE; $user->save(); $user = new User(); $user->email = '*****@*****.**'; $user->password = Hash::make('usuariousuario'); $user->username = '******'; $user->admin = FALSE; $user->editor = FALSE; $user->active = TRUE; $user->save(); }
public function twitter(Request $request) { $requestTokenUrl = 'https://api.twitter.com/oauth/request_token'; $accessTokenUrl = 'https://api.twitter.com/oauth/access_token'; $profileUrl = 'https://api.twitter.com/1.1/users/show.json?screen_name='; $client = new GuzzleHttp\Client(); // Part 1 of 2: Initial request from Satellizer. if (!$request->input('oauth_token') || !$request->input('oauth_verifier')) { Log::info(Config::get('app.twitter_callback')); $requestTokenOauth = new Oauth1(['consumer_key' => Config::get('app.twitter_key'), 'consumer_secret' => Config::get('app.twitter_secret'), 'callback' => Config::get('app.twitter_callback')]); $client->getEmitter()->attach($requestTokenOauth); // Step 1. Obtain request token for the authorization popup. $requestTokenResponse = $client->post($requestTokenUrl, ['auth' => 'oauth']); $oauthToken = array(); parse_str($requestTokenResponse->getBody(), $oauthToken); // Step 2. Send OAuth token back to open the authorization screen. return response()->json($oauthToken); } else { $accessTokenOauth = new Oauth1(['consumer_key' => Config::get('app.twitter_key'), 'consumer_secret' => Config::get('app.twitter_secret'), 'token' => $request->input('oauth_token'), 'verifier' => $request->input('oauth_verifier')]); $client->getEmitter()->attach($accessTokenOauth); // Step 3. Exchange oauth token and oauth verifier for access token. $accessTokenResponse = $client->post($accessTokenUrl, ['auth' => 'oauth'])->getBody(); $accessToken = array(); parse_str($accessTokenResponse, $accessToken); $profileOauth = new Oauth1(['consumer_key' => Config::get('app.twitter_key'), 'consumer_secret' => Config::get('app.twitter_secret'), 'oauth_token' => $accessToken['oauth_token']]); $client->getEmitter()->attach($profileOauth); // Step 4. Retrieve profile information about the current user. $profile = $client->get($profileUrl . $accessToken['screen_name'], ['auth' => 'oauth'])->json(); Log::info('Twitter'); Log::info($profile); // Step 5a. Link user accounts. if ($request->header('Authorization')) { $user = User::where('twitter', '=', $profile['id']); if ($user->first()) { return response()->json(['message' => 'There is already a Twitter account that belongs to you'], 409); } $token = explode(' ', $request->header('Authorization'))[1]; $payload = (array) JWT::decode($token, Config::get('app.token_secret'), array('HS256')); $user = User::find($payload['sub']); $user->twitter = $profile['id']; $user->username = $user->username ?: $profile['screen_name']; $user->save(); return response()->json(['token' => $this->createToken($user)]); } else { $user = User::where('twitter', '=', $profile['id']); if ($user->first()) { return response()->json(['token' => $this->createToken($user->first())]); } $user = new User(); $user->twitter = $profile['id']; $user->username = $profile['screen_name']; $user->save(); return response()->json(['token' => $this->createToken($user)]); } } }