Westagram - 1.login page [React]

quokkaยท2021๋…„ 11์›” 4์ผ
0

Project

๋ชฉ๋ก ๋ณด๊ธฐ
3/6
post-thumbnail

# ๐ŸŒˆ Instagram clone์ฝ”๋”ฉ์„ ํ•ด๋ณด์•˜๋‹ค.

๐ŸŒ€ login page๋ฅผ ์ฒ˜์Œ html/css/javascript๋กœ ๋งŒ๋“ค์—ˆ์—ˆ๋‹ค. ๊ทธ๊ฑธ react๋กœ ์ฝ”๋“œ๋ฅผ ๋ณ€ํ˜•์‹œ์ผœ์„œ ๋งŒ๋“ค์–ด ๋ณด์•˜๋‹ค.

(์ด๋ฏธ์ง€๋‚˜ ๊ตฌ์„ฑ์€ ๋˜‘๊ฐ™์•„์„œ ์‚ฌ์ง„์€ ์ƒ๋žตํ•ฉ๋‹ˆ๋‹น )

import React, { Component } from 'react';
import './Login.scss';

class LoginJihoo extends React.Component {
  constructor() {
    super();
    this.state = {
      id: '',
      password: '',
    };
  }

  // goToMain = () => {
  //   this.props.history.push('/main-jihoo');
  // };

  submitLoginInfo = () => {
    // console.log('ํ•จ์ˆ˜ํ˜ธ์ถœ ์„ฑ๊ณต');
    fetch('http://10.58.0.69:8000/user/signin', {
      method: 'POST',
      body: JSON.stringify({
        // => ํ”„๋ก ํŠธ์—์„œ ๋ฐฑ์œผ๋กœ ์ •๋ณด๋ฅผ ์ฃผ๋Š”๊ฒƒ
        email: this.state.id,
        password: this.state.password,
      }),
    })
      .then(res => res.json()) //์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋กœ ๋ฐ”๊ฟ”์ค€๋‹ค.
      .then(result => {
        // const { history } = this.props;  //history๋Š” ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ ์‚ฌ์šฉํ•  ใ…
        if (result.message === 'INVALID_USER') {
          // ๋งž์ง€ ์•Š๋Š” ์œ ์ €
          alert('๋‹ค์‹œ ๋กœ๊ทธ์ธํ•ด์ฃผ์„ธ์š”');
        } else if (result.access_token) {
          localStorage.setItem('token', result.access_token);
          this.props.history.push('/main-jihoo');
        }
      });
  };

  handleIdInput = e => {
    //IdInput์€ event์˜ ์•ฝ์ž e๋ฅผ ์ธ์ž๋กœ ๋ฐ›์Œ
    this.setState({
      id: e.target.value, // console.log-> setState์ดํ›„๋ผ๊ณ  ๋‹ค ์‹คํ–‰๋œ๊ฒƒ์€ ์•„๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ render๋ฐ‘์—์„œ console.log
    });
  };

  handlePwInput = e => {
    this.setState({
      password: e.target.value,
    });
  };

  render() {
    const okayId = this.state.id.indexOf('@');
    const okayPw = this.state.password.length > 5;
    return (
      <section className="login">
        <h2 className="login-title">WESTAGRAM</h2>
        <div className="input-container">
          <div className="input-wrap">
            <input
              id="id"
              type="text"
              placeholder="์ „ํ™”๋ฒˆํ˜ธ,์‚ฌ์šฉ์ž์ด๋ฆ„ ๋˜๋Š” ์ด๋ฉ”์ผ"
              onChange={this.handleIdInput}
            />
            <input
              id="password"
              type="password"
              placeholder="๋น„๋ฐ€๋ฒˆํ˜ธ"
              onChange={this.handlePwInput} // ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•œ ๊ฒฝ์šฐ์—๋Š” ์ธ์ž๋ฅผ ์„ ์–ธํ•ด์•ผ๋˜์ง€๋งŒ, event์‹œ onchange,
              //onclick์ž์ฒด์— ๊ธฐ๋ณธ๊ฐ’์œผ๋กœ ์ธ์ž๊ฐ€ ์„ค์ •๋˜์–ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์‚ฌ์šฉ ํ•˜์ง€ ์•Š์•„๋„ ์ƒ๊ด€ ์—†๋‹ค.
            />
          </div>
          <button
            className={
              okayId !== -1 && okayPw
                ? 'changeButtonColor btn'
                : 'normalButtonColor btn'
              // css ํŒŒ์ผ๋กœ ๋‹ค์‹œ ์ •ํ•˜๊ธฐ.
              //validation ? "able" : "disable"
            }
            onClick={this.submitLoginInfo}
            disabled={!(okayId !== -1 && okayPw)}
          >
            ๋กœ๊ทธ์ธ
          </button>
        </div>
        <div className="last">
          <p>
            <a href="/">๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์žŠ์œผ์…จ๋‚˜์š”?</a>
          </p>
        </div>
      </section>
    );
  }
}

export default LoginJihoo;

๐Ÿ‘‰ state์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•ด์„œ ๊ด€๋ฆฌํ•œ๋‹ค.

๐Ÿ‘‰ state์—์„œ ์ €์žฅํ•œ ๋ฐ์ดํ„ฐ๋ฅผ handleIdInput์—์„œ setState๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ณ€๊ฒฝํ•˜๊ณ (event)๋ฅผ ์‚ฌ์šฉํ•ด์„œ input๊ฐ’์— ๋„ฃ์„ ์ˆ˜ ์žˆ๊ฒŒ ํ•จ์ˆ˜๋ฅผ ์งฐ๋‹ค.

๐Ÿ‘‰ react๋กœ ํŽ˜์ด์ง€๋ฅผ ๊ตฌ์„ฑํ•œ ๋‹ค์Œ์— ๋ฐฑ์—”๋“œ์™€ ํ†ต์‹ ์„ ํ•ด์„œ ํšŒ์›๊ฐ€์ž…/๋กœ๊ทธ์ธ์ด ๋˜๋Š”์ง€ ํ™•์ธํ•ด๋ณด์•˜๋‹ค.
๐Ÿ‘‰ ๋ฐฑ์—”๋“œ์™€ ํ†ต์‹ ํ•  ๋•Œ๋Š” fetch๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์„œ๋ฒ„์ฃผ์†Œ๋ฅผ ๋ฐ›์•„์˜ค๊ณ  ํ”„๋ก ํŠธ์—์„œ๋„ ์ •๋ณด๋ฅผ ์ฃผ๊ณ  ๋ฐ›์•„์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— post๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ๊ฒŒ ๋˜์—ˆ๋‹ค.
๐Ÿ‘‰ ๊ทธ๋Ÿฐ๋‹ค์Œ ํšŒ์›๊ฐ€์ž…์„ ํ•œ ์œ ์ €์ธ์ง€ ์•„๋‹Œ์ง€๋ฅผ ํŒ๋‹จํ•ด์„œ ๋กœ๊ทธ์ธ ์—ฌ๋ถ€๋ฅผ ์„ฑ๊ณตํ•ด์•ผ๋˜๊ธฐ ๋•Œ๋ฌธ์— token๊ฐ’์œผ๋กœ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋Š” ๋กœ์ง์„ ์งœ ๋ณด์•˜๋‹ค.

profile
๐Ÿ‘ฉ๐Ÿปโ€๐Ÿ’ป ๋งค์ผ๋งค์ผ์ด ๊ธฐ๋Œ€๋˜๋Š” ๊ฐœ๋ฐœ์ž ^^

0๊ฐœ์˜ ๋Œ“๊ธ€