by Devin Yang
(This article was automatically translated.)

Published - 2 years ago ( Updated - 2 years ago )

Laravel provides quite a few useful packages, Passport is one of them,
When the website needs to set up its own OAuth2 verification host, it really saves time and effort.
This site is an example, because the front-end and back-end separation mechanism is adopted (there are two different Laravel projects), and I verify through the front-end WEB.
Basically, I have many other different projects, and they all rely on OAuth2 provided by Laravel to handle cross-domain authentication.

When I log in to the WEB at the front desk, I can log in directly by opening other projects.
For example, the posting background and chat room of this site are all authenticated by home OAuth2.

Having said so much, let’s look at the commands I most often issue as follows. In the early days, Laravel also provided WEB UI components, but it seems to be gone later, because it seems that it is unnecessary. 😅

php artisan passport:client

 Which user ID should the client be assigned to?: (Enter the user ID here)
 >9

 What should we name the client?: (The new project name will be displayed to the user when logging in. The app with this name needs to be verified. Basically, I directly AutoSubmit@@)
 > New project

 Where should we redirect the request after authorization? [https://www.ccc.tc/auth/callback]:
 > https://app.ccc.tc/auth/ccc/callback


New client created successfully.
Client ID: n 
Client secret: 

See the last screen above -, if you often build OAuth verification, you should be familiar with it, like Facebook's OAuth verification or Google's OAuth verify.
To put it bluntly, with Passport installed, we have our own OAuth2 verification host instead of using Facebook or Google.
My chat room host is a large hybrid type, which supports different verifications, and can chat after logging in.

Let me share the naming method in my .env, you will find that almost all envs are almost the same, just like new brothers...:p
ID, SECRET and CALLBACK.

#CCC OAuth
CCC_OAUTH_SERVER=https://www.ccc.tc
CCC_CLIENT_ID=
CCC_SECRET=
CCC_CALLBACK_URL=${APP_URL}/auth/ccc/callback

#Line Notify
LINE_NOTIFY_CLIENT_ID=
LINE_NOTIFY_SECRET=
LINE_NOTIFY_CALLBACK_URL=${APP_URL}/auth/line/notify/callback

#BOT
LINE_CHANNEL_ID=
LINE_CHANNEL_SECRET=
LINE_CALLBACK_URL=${APP_URL}/auth/line/callback

FACEBOOK_CLIENT_ID=
FACEBOOK_SECRET=
FACEBOOK_CALLBACK_URL=${APP_URL}/auth/facebook/callback

GITHUB_CLIENT_ID=
GITHUB_SECRET=
GITHUB_CALLBACK_URL=${APP_URL}/auth/github/callback

GOOGLE_CLIENT_ID=
GOOGLE_SECRET=
GOOGLE_CALLBACK_URL=${APP_URL}/auth/google/callback


If you also try to install Laravel's passport, the current new version of Laravel will use sanctum by default, remember to call back the api.

Route::middleware('auth:sanctum')->get('/user', function (Request $request) {
    return $request->user();
});


For detailed installation and settings of Laravel Passport, please refer to the manual on the official website:
https://laravel.com/docs/9.x/passport

* 100043*

Tags: laravel oauth2

Devin Yang

Feel free to ask me, if you don't get it.:)

No Comment

Post your comment

Login is required to leave comments

Similar Stories


laravel,dotenv,seo

How do I dynamically load different dotenv files in Laravel according to the subdomain name

Before we start, let me complain. Originally, my website could automatically display different languages ​​according to the user's browser. That's okay, but Google's SEO doesn't seem to like it. He recommends using different URLs for each language and not using cookies or browser settings to adjust the content of the page. Well, I'll just be obedient. His suggested method, the first solution: distinguish by country, such as example.tw or example.de, how is this really impossible, or buy the registered domain name, or take all the domain names and no one will take it The strange domain name is more likely. The second solution: use sub-domain names to distinguish, this is what I am going to do, and so on to explain how to do it in Laravel. The third solution: example.com/tw/ and the like, Apple seems to do it this way. The fourth solution: site.com?loc=tw and the like are not recommended, indeed I think this is not a good idea.

laravel storage

How do I sync files with Laravel's Storage's sftp driver

The installation and setting of SFTP is beyond the scope of this article. If you want to know more, I suggest you read another article https://www.ccc.tc/article/Laravel-SFTP-Driver-Configuration. In this article, I will use a very fast Sample. Demo How do I use the self-built artisan command to synchronize the remote data to the local end. Some people may ask why rsync is not used. This article is to introduce the use of Storage to synchronize files. 🤪Let’s look directly at the code, I set up an sftp disk called ccc in config/filesystems.php.

laravel,laravel livewire

Laravel livewire file upload demo

Livewire is a full-stack framework for the Laravel framework. It was the first time I wrote code, and he was probably the one who was able to write and marvel at the magic. My colleague also marveled while writing after being pushed by me. When I first learned Vue, I was not so surprised by him.