In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
The main content of this article is "how to achieve login for development only", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how to achieve login for development only"!
First add this route to the main () function.
Router.HandleFunc ("POST", "/ api/login", requireJSON (login)) login
This function handles POST requests to / api/login, where JSON body contains only the user name and returns the authenticated user, token, and expiration date in JSON format.
Func login (w http.ResponseWriter, r * http.Request) {if origin.Hostname ()! = "localhost" {http.NotFound (w, r) return} var input struct {Username string `json: "username" `} if err: = json.NewDecoder (r.Body). Decode (& input) Err! = nil {http.Error (w, err.Error (), http.StatusBadRequest) return} defer r.Body.Close () var user User if err: = db.QueryRowContext (r.Context (), `SELECT id, avatar_url FROM users WHERE username = $1`, input.Username). Scan (& user.ID, & user.AvatarURL,) Err = = sql.ErrNoRows {http.Error (w, "User not found", http.StatusNotFound) return} else if err! = nil {respondError (w, fmt.Errorf ("could not query user:% v", err)) return} user.Username = input.Username exp: = time.Now () .Add (jwtLifetime) token, err: = issueToken (user.ID, exp) if err! = nil {respondError (w) Fmt.Errorf ("could not create token:% v", err) return} respond (w, map [string] interface {} {"authUser": user, "token": token, "expiresAt": exp,}, http.StatusOK)}
First, it checks whether we are on the local host, or if the response is 404 Not Found. It decodes the body to skip validation because it is only for development purposes. The user with the given user name is then queried in the database, and if not, 404 NOT Found is returned. It then uses the user ID as the subject to issue a new JSON Web token.
Func issueToken (subject string, exp time.Time) (string, error) {token, err: = jwtSigner.Encode (jwt.Claims {Subject: subject, Expiration: json.Number (strconv.FormatInt (exp.Unix (), 10),}) if err! = nil {return ", err} return string (token), nil}
This function performs the same operation as above. I just moved it over to reuse the code.
After you create a token, it responds with the user, token, and expiration date.
Seed user
Now you can add the user you want to operate to the database.
INSERT INTO users (id, username) VALUES (1, 'john'), (2,' jane')
You can save it to a file and pipe it to Cockroach CLI.
Cat seed_users.sql | cockroach sql-- insecure-d messenger
okay. Once the code is deployed to the production environment and uses its own domain, the login feature will not be available.
At this point, I believe you have a deeper understanding of "how to achieve login for development only". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 301
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.