Login programmatically as a user | Drupal 8

Login programmatically as a user

Submitted by editor on Wed, 12/09/2015 - 17:25
Question

How to login programmatically as a user  in Drupal 8 ?
How to autologin in D8?

Example:
$user = User::load($uid);
user_login_finalize($user);

Comments

How do you trigger this way of login? Does the code need to be in the controller?

Vindesh mohariya (not verified)

Mon, 08/01/2016 - 12:55

Sometimes we need to login into our Drupal site not through the regular login form. For example if we have trouble with login form or maybe we just forgot our password... Here are few ways to login into your Drupal site not through login form.

For Drupal 8 you just need to create file your_auto_login.php in the Drupal root directory, put there the next code and open this file in your browser:

<?php

use Drupal\Core\DrupalKernel;
use Symfony\Component\HttpFoundation\Request;

$autoloader = require_once 'autoload.php';

$kernel = new DrupalKernel('prod', $autoloader);

$request = Request::createFromGlobals();
$response = $kernel->handle($request);

// ID of the user.
$uid = THE_AUTOMATIC_USER_ID; //Do not use 1 because it is the super admin
$user = Drupal\user\Entity\User::load($uid);
user_login_finalize($user);

$response->send();

$kernel->terminate($request, $response);

Hi,

When I am using this code in a custom module, I am getting below error

"InvalidArgumentException: Cannot redirect to an empty URL. in Symfony\Component\HttpFoundation\RedirectResponse->setTargetUrl() (line 76 of /websites/www/html/drl/vendor/symfony/http-foundation/RedirectResponse.php) #0 /websites/www/html/drl/vendor/symfony/http-foundation/RedirectResponse.php(39): Symfony\Component\HttpFoundation\RedirectResponse->setTargetUrl(NULL) #1 /websites/www/html/drl/sites/grants/themes/grants/grants.theme(1536): Symfony\Component\HttpFoundation\RedirectResponse->__construct(NULL) #2 [internal function]: custom_login_user_login(Object(Drupal\user\Entity\User)) #3 /websites/www/html/drl/core/lib/Drupal/Core/Extension/ModuleHandler.php(402): call_user_func_array('custom_login_us...', Array) #4 /websites/www/html/drl/core/modules/user/user.module(559): Drupal\Core\Extension\ModuleHandler->invokeAll('user_login', Array) #5 /websites/www/html/drl/your_auto_login.php(12): user_login_finalize(Object(Drupal\user\Entity\User)) #6 {main}."

 

Can you please help me where the thing is going wrong.

red bottom shoes
cartier glasses
moncler
mulberry
moncler,moncler jacket
salomon boots
ferragamo shoes men
coach outlet
skechers women
true religion hoodie
salomon boots
nike air max 97
ed hardy clothing
jordan retro 11
lebron 15
ugg boots
pandora jewelry
nike air max 270 women
yeezy
kate spade
under armour outlet
asics womens running shoes
adidas ultra boost uncaged
mont blanc sale
goyard
moncler coat
vapormax
north face
fitflops uk
pandora necklace
james harden
true religion outlet
jordan shoes
uggs for women
lebron 15
adidas outlet
kd 10
barbour coats
chrome hearts sunglasses
chrome hearts bracelet
ralph lauren
adidas nmd r1
christian louboutin shoes
yeezy
oakley sunglasses
huaraches
air max 90
kate spade outlet
burberry scarf
nike air max 95
fitflop sale
salvatore ferragamo
michael kors purse
adidas originals ultra boost
burberry
stephen curry shoes
fitflops clearance
yeezy
nike air max
kevin durant shoes
celine wallet
ray ban sunglasses
kyrie irving
air max 97 womens
nike air max
nike air max 97
louboutin
ugg boots
Yeezy Boost 350
supreme jacket
yeezy
adidas originals superstar
mbt mens shoes
barbour wax jacket
nike air vapormax
pandora
jimmy choo shoes
jordan 11 space jam
moncler sale
lacoste shoes
skechers boots
yeezy boost 350
swarovski pen
nike air max
ralph lauren shirts
north face jacket
chrome hearts t shirt
ray ban wayfarer
goyard
pandora charms
jordan 11
supreme shirt
nike air max 90
pandora jewelry official site
birkenstock sandals
longchamp
uggs on sale
mcm wallet
kd 11
nike air max
bulgari jewelry
moncler jacket
salomon shoes
kyire 3
yeezy boost 350 v2
air max 95
Pandora Rings
valentino sandals
red bottoms
moncler jacket
Hermes Wallets
space jam 11
nike air max 270
nike air max
ralph lauren polo shirts
new balance golf shoes
stephen curry shoes
adidas nmd
nmd
pandora bracelet
ralph lauren, polo
nike air max 95
prada purse
swarovski earrings
adidas ultra boost
burberry handbags
mulberry bag
asics shoes
nike air max 270
coach bags
longchamp
ray ban
Salomon
michael kors purse
skechers
fenty puma shoes
pandora jewelry
mcm backpack
pandora bracelet
kate spade bags
nike air max 90
balenciaga sneakers
balenciaga shoes
oakley prescription sunglasses
pandora jewelry
fitflop shoes
nike air max 95 mens
ray ban glasses
pandora charms
jordan shoes
birkenstock sale
nmd
nike air huarache
ecco boots
prada handbags
nike shoes
hermes belt women
pandora jewelry

Raghvendra Gupta (not verified)

Thu, 08/04/2016 - 15:00

You need to put index.php at root
$uid ='Your User ID Here';
$account = \Drupal\user\Entity\User::load($uid);
\Drupal::service('session')->migrate();
\Drupal::service('session')->set('uid', $account->id());
\Drupal::moduleHandler()->invokeAll('user_login', array($account));

or
$uid ='Your User ID Here';
$user = Drupal\user\Entity\User::load($uid);
user_login_finalize($user);

$response->send();

chenjinyan (not verified)

Mon, 10/22/2018 - 09:43

adidas stan smith

prada outlet

michael kors purse

tory burch outlet

manolo blahnik

ferragamo shoes

coach outlet

michael kors outlet

ugg outlet

nfl jerseys

sac longchamp

fitflops

true religion outlet

polo outlet

ysl handbags

fila shoes

canada goose outlet

michael kors outlet

nike air max 270

salvatore ferragamo

mont blanc

belstaff outlet

michael kors handbags

kate spade outlet

north face outlet

nicholas kirkwood

herve leger dresses

nike blazer

hundreds clothing

x-large clothing

woolrich jackets

roshe run

nike flight bonafide

jimmy choo

soccer jerseys

jordans

adidas jeremy scott

longchamp

air jordan

north face jackets

moncler outlet

tiffany jewelry

coach outlet

jordans

fitflops

burberry

sophia webster shoes

ugg boots

michael kors outlet

ugg store

celine

kate spade handbags

nike presto

supreme clothing

vapormax

ugg slippers

timberland outlet

canada goose outlet

cheap nfl jerseys

coach outlet

nike air max

nike cortez

undefeated clothing

coach outlet

ugg for kids

adidas outlet

spalding basketball

vetements clothing

polo outlet

supra shoes

supreme new york

nike outlet/strong>

converse shoes

oakley sunglasses

nike shoes for women

prada outlet

miu miu handbags

ray ban

ralph lauren outlet

michael kors

birkenstock

beats by dre

mikimoto jewelry

ralph lauren

soccer jerseys

y3 shoes

mbt

under armour outlet

ray ban sunglasses

adidas football boots

pandora jewelry

uggs official site

supreme

north face

the north face

puma fenty

off-white

ugg sale

michael kors outlet clearance

polo ralph lauren

201810.22chenjinyanc

Add new comment