Welcome to Rants and Raves!
I kept hearing that I needed to write, so here is the blog that I ended up starting.
It is mostly an accounting of my progression through creating a web site, nuances with code, and tips about what I did to make things work.
In there you will find sprinkled about some gems of life, and letting loose fun.
Enjoy!
Implementing Google Sign In
2023-02-24
I've been working on integrating logging in with Google, this is relatively easy with Laravel's Socialite:
https://laravel.com/docs/8.x/socialite
I did initially use this site, which walks you through a basic setup but with outdated Google Developer Console instructions:
https://medium.com/employbl/add-login-with-google-to-your-laravel-app-d2205f01b895
For better Google Developer Console instructions, this site is more relevant:
https://shouts.dev/articles/laravel-socialite-login-with-google-account-with-breeze
There are many different ways to handle the callback and what you do with the information provided by the social provider. This sites I referenced do more of a simple check against just the email or Google ID, which they include as another column in the user table. This way seemed a little overly simple, and messy for the user table. After doing a little bit of searching for a more thorough socialite integration backend setup I ran across this thread, which is a little dated for the code but has solid concepts:
https://laracasts.com/discuss/channels/requests/complete-register-and-login-app-with-socialite-and-multiple-providers
I liked the general consensus that the social profile information should be in it's own table and tied to the appropriate user ID as well as the providers, in their own table too. This seemed like a good way to allow flexibility to add more social providers, and have them all be able to be linked to the same user on my site. This does assume that each of the social accounts use the same base email, which I will probably look into allowing the linking of multiple emails and checks to see if social emails have changed. I did notice in the provided information that Google provides a 'verified_email' Boolean, soo I threw in a quick conditional on that as well. So in my callback I ended up with something like:
try {
$socialUser = Socialite::driver('google')->user();
} catch (\Exception $e) {
return redirect('/login');
}
if($socialUser->user['verified_email']){
$spid = SocialProvider::where('name','like','Google')->first()->id;
$socialProfile = SocialProfile::where('provider_id', $spid)->where('social_id', $socialUser->id)->first();
if(!$socialProfile){
$user = User::where('email', $socialUser->email)->first();
if(!$user){
$user = new User;
...
$user->save();
event(new Registered($user));
}
$socialProfile = new SocialProfile;
...
$socialProfile->save();
}else{
$user = User::where('id', $socialProfile->user_id)->first();
}
if($user){
auth()->login($user, true);
return redirect()->intended("/user");
}else{
return redirect('/login');
}
}else{
return redirect('/login');
}
}
Oh yeah, I also stole the "event(new Registered($user));" code from the Laravel Breeze registration controller to trigger the email verification email. Since my code currently checks that, and it still seems like a good thing to have in place.
Ride Through That Storm
2023-02-23
A bit stormy today. It snowed all night, and it's been precipitating all day across several states. I say precipitating because it started as snow, but as we got a little bit South and more East it transitioned through sleet to rain. A bit gloomy I suppose, and there was perhaps a little bit of potential for crashes if the storm wasn't being respected. But really, there wasn't much to worry about if you used a little bit of caution. Most people have a tendency to overly fear a little bit of bad weather, and then there are those few that don't give enough respect. It is hard to get just the right amount of caution, especially when there are others on the road. On the other hand it is easier to gauge how much respect to give when you see the others that didn't and are now on the side of the road. I will give props to those that do a good job of helping keep the roads clear and help those that are unfortunate enough to suffer at the hands of the inclamite weather.
Yay... Job Hunting...
2023-02-22
I have started applying for official jobs again, it is not something I enjoy doing. Often it's a lot of jumping through hoops and bending over backwards for nothing. One of the things that I like least is experience mismatch. There are plenty of things where I just simply haven't spent enough time doing the one thing, that's understandable. The problem is when a company wants a decade of experience for an entry level position, and because of that the entry level person can't get the experience. I do understand that companies want the best they can get, and don't want to spend a lot of time and money having to train up someone enough to start benefiting from the new hire. But they need to realize that there is a difference between entry level experience and pay. It seems to me that they are trying to replace a vacancy with the same level of experience, but not give the same amount of pay because they are a new hire and thus "entry level". I do appreciate the companies that are willing to read between the lines and look at the candidate as a whole, which I also realize can be a challenge. There are many places that are overwhelmed with applicants which is why so many of the resume sites emphasize that you have 10-30 seconds to impress HR, if they even see your application. Then again, it's always been about having connections and who you know, hasn't it? Honestly, the whole thing's a mess and there's not really anything I can do to change it, just gotta play the games.
Laravel Accessors And Mutators
2023-02-21
Earlier I made note of adding functions to a model in order to do something like make a full name from the first and last name, but there's more that can be done there. Laravel Accessors & Mutators are predefined-ish functions that can be used to manipulate the DB data when accessing or saving it, respectively. Using these you can make sure all of your blog post titles are capitalized before presenting them:
public function getTitleAttribute($value)
{
return ucwords($value);
}
Or make sure an email is all lower case before saving it:
public function setEmailAttribute($value)
{
$this->attributes['email'] = strtolower($value);
}
This format is good for Laravel 8, but definitely not for Laravel 10.
https://laravel.com/docs/8.x/eloquent-mutators
You can technically do a lot more inside of the function before it returns or saves the data, but I anteed to make a note about the functions inside of the accessor and mutator here and in the Laravel docs. These are using built in PHP functions, not the Laravel helper functions.
https://www.php.net/manual/en/ref.strings.php
https://laravel.com/docs/8.x/helpers
If you use the Laravel helper functions you will need to add the appropriate namespaces, but not if you can use the PHP ones.
Redirect To Intended With 'Auth' Middleware
2023-02-20
I was having an issue where my login would timeout and after logging in it would not redirect me back to the intended page I was trying to access. I had assigned the 'verified' middleware to a group of web routes, and that did handle the security of needing to be logged in and verified to access the resource, but wasn't doing the redirect. After a little research it seemed like I needed to add the 'auth' middleware to the group. So I went from:
Route::middleware(verified)->group(function(){ ... });
To:
Route::middleware(['auth','verified'])->group(function(){ ... });
I tried to find some documentation to confirm that having multiple middleware defined like this worked as an AND or an OR, but it the small looking I did was not definitave. I did my own test and determined that they work as an AND, the user needs to have an account for the 'auth' AND needs to have that email vervied for the 'verified' to access anything within that group.
https://laravel.com/docs/10.x/middleware#assigning-middleware-to-routes
https://laracasts.com/discuss/channels/laravel/redirect-intended-not-working-after-login?page=1
https://stackoverflow.com/questions/40822882/adding-multiple-middleware-to-laravel-route
Use String Matching For Conditional
2023-02-19
If you want to do a partial string comparison as your conditional condition you can simply use:
stristr('thing to look in', 'thing to look for')
If the second item is located in the first then it should return true, otherwise it should return false. So you can end up with something like:
if(stristr('thing to look in', 'thing to look for')){
match found
}else{
no match
}
https://www.php.net/manual/en/function.stristr.php
Functions In Laravel Models Make Life Easier
2023-02-18
If you have some information broken out in the DB but is often good to have combined, such as names, you can make a function in the model to do so:
public function fullName() {
return $this->firstName . ' ' . $this->middleName . ' ' . $this->lastName;
}
And then use the function in the rest of your code:
$user->fullName();
https://laracasts.com/discuss/channels/eloquent/how-to-combine-firstname-lastname-and-middlename
This idea could be used to centralize functions that you find you often do with a model.
Use Laravel Table Schema Builder In Controller
2023-02-17
It is possible to manipulate the DB in the Laravel controller just like you can in the migrations. Just add:
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
to the beginning of the controller and you can utilize the:
Schema::create/table('table_name', function (Blueprint $table) {
$table->id();
...
$table->timestamps();
});
format.
https://laravel.com/docs/9.x/migrations#migration-structure
https://laracasts.com/discuss/channels/general-discussion/how-to-include-schema-builder-in-a-controller
Back To Final Fantasy XI
2023-02-16
Well I am now officially paying for FFXI. Spent some time playing and getting a feel for the system, luckily my internet connection was solid enough until just before I was going to quit anyway. Odd things in how it performs, like crashing if you switch to another program. Not sure if that's on purpose, but it would be nice if you could look at a webpage without having to completely log in again. It would also be kind of nice if there was the option to toggle the expansions. There's enough going on to start with that getting bombarded with tidbits of story arcs that I am not going to be able to keep track of until the next instalment happens. Anyway, I got a few levels and my first trust while working through the first starter adventure quests.
Coloring Of The Navbar
2023-02-15
Bootstrap has some nice color presets for buttons, but a little bit more complex when it comes to the navbars. By default the navbar utilizes the light class and you can change to the navbar-dark class for dark backgrounds. These are to be used with the background color bg-* classes, or you can use inline styling to enter a custom color code.
https://getbootstrap.com/docs/5.2/components/navbar/#color-schemes
If you are looking for more customization of the colors here or in general you should definitely look into playing with the CSS variables themselves. I have not done so yet, but it is definitely a potentially powerful tool.
https://getbootstrap.com/docs/5.2/components/navbar/#css