2. Join - asdfgl98/Project-CodeBuddy GitHub Wiki

πŸ“ŒJoin

βœ… Join(νšŒμ›κ°€μž…) μ£Όμš” κΈ°λŠ₯

  • axiosλ₯Ό μ‚¬μš©ν•΄ 비동기 λ°©μ‹μœΌλ‘œ 아이디 쀑볡확인을 ν•˜κ³ , 아이디 μ‚¬μš©μ΄ κ°€λŠ₯ν•  λ•Œ νšŒμ›κ°€μž… 처리λ₯Όν•©λ‹ˆλ‹€.
  • ID쀑볡확인을 클릭 μ‹œ, axios둜 쀑볡확인 λΌμš°ν„°μ— μž…λ ₯ν•œ IDλ₯Ό ν¬ν•¨ν•˜μ—¬ μš”μ²­μ„ 보내 λΌμš°ν„° λ‚΄λΆ€μ—μ„œ DB와 μ—°κ²°ν•˜μ—¬ κ²€μ¦ν•©λ‹ˆλ‹€.
  • DB에 μ—†λŠ” 아이디 -> μ‚¬μš©μ΄ κ°€λŠ₯ν•œ 아이디 μž…λ‹ˆλ‹€. 좜λ ₯
  • DB에 μžˆλŠ” 아이디 -> μ€‘λ³΅λœ 아이디 μž…λ‹ˆλ‹€. 좜λ ₯
  • 곡백이 μžˆκ±°λ‚˜, 쀑볡 확인을 μ•ˆν–ˆμ„ λ•Œ νšŒμ›κ°€μž… 클릭 μ‹œ - > ID 확인 λ˜λŠ” λΉˆμΉΈμ„ ν™•μΈν•΄μ£Όμ„Έμš”. 좜λ ₯

πŸ’» CodeDetail

νšŒμ›κ°€μž… 아이디 검증 둜직


public -> js -> join.js


// μ§€ν›ˆ μˆ˜μ • - νšŒμ›κ°€μž… 아이디 검증 둜직

const idBtn = document.getElementById('idBtn')
const idCk = document.getElementById('idCk')
const subBtn = document.getElementById('subBtn')
const idInput = document.querySelector('#inputId')
const pwInput = document.querySelector('#pwInput')
const nameInput = document.querySelector('#userName')
const nickInput = document.querySelector('#userNick')
// 아이디 쀑볡체크 확인여뢀
let dupCk = false
// 아이디 μ‚¬μš©κ°€λŠ₯ μ—¬λΆ€
let idUse = false

//아이디 쀑볡검사 
function idCheckHandler(){
    const data = document.querySelector('#inputId').value
    axios.post('/user/idCheck', {idCheck : data})
        .then(res=>{
            if(res.data){
                if(data == ''){
                    idCk.innerText = '아이디λ₯Ό μž…λ ₯ν•΄μ£Όμ„Έμš”.'
                    idCk.style.color = 'red'
                    idUse = false
                }
                else{
                    idCk.innerText = 'μ‚¬μš©μ΄ κ°€λŠ₯ν•œ 아이디 μž…λ‹ˆλ‹€.'
                    idCk.style.color = 'green'
                    idUse = true
                    checkMsg.innerText = ''
                }                            
            }
            else{
                idCk.innerText = 'μ€‘λ³΅λœ 아이디 μž…λ‹ˆλ‹€.'
                idCk.style.color = 'red'
                idUse = false
            }
        })
        dupCk = true           
}


// 아이디 μ€‘λ³΅λ²„νŠΌ 클릭 μ‹œ 쀑볡검사 
idBtn.addEventListener('click',idCheckHandler)

// 아이디 μˆ˜μ •μ‹œ 쀑볡체크여뢀 μ΄ˆκΈ°ν™”
idInput.addEventListener('input',()=>{
    dupCk = false
})

// νšŒμ›κ°€μž… ν΄λ¦­μ‹œ
subBtn.addEventListener('click',(event)=>{
    // 쀑볡체크와 아이디 μ‚¬μš©κ°€λŠ₯ μ—¬λΆ€ 확인 후에 폼 μ œμΆœν•˜κΈ° μœ„ν•΄ 폼 제좜 κΈ°λŠ₯ 차단
    event.preventDefault()
    if(dupCk == false){
        checkMsg.innerText = '아이디 쀑볡체크λ₯Ό ν•΄μ£Όμ„Έμš”'
        checkMsg.style.color = 'red'
    }
    else{
        if(idUse == false){
        checkMsg.innerText = '아이디λ₯Ό ν™•μΈν•΄μ£Όμ„Έμš”'
        checkMsg.style.color = 'red'
        }
    }
    if(pwInput.value == '' || nameInput.value == '' || nickInput.value == ''){
        checkMsg.innerText = 'ID 확인 λ˜λŠ” λΉˆμΉΈμ„ ν™•μΈν•΄μ£Όμ„Έμš”'
        checkMsg.style.color = 'red'
    }
    if(dupCk && idUse && pwInput.value != '' && nameInput.value != '' && nickInput.value != ''){
        // 쀑볡체크 확인과 아이디 μ‚¬μš© κ°€λŠ₯ μ‹œ form 제좜
        const myForm = document.getElementById('myform');
        myForm.submit(); // 폼 제좜

        // νšŒμ›κ°€μž… μ„±κ³΅μ‹œ μžλ™ 둜그인 λ²„νŠΌ 클릭
        axios.post('/user/checkend',{ck : 'hello'})
        .then(res=>{
            if(res.data.success){
                document.getElementById('loginCk').click()
            }
            else{
                console.log('μ—λŸ¬',res.data)
            }
        })
    }
})
아이디 쀑볡체크 및 νšŒμ›κ°€μž… 성곡 λΌμš°ν„°


routes -> user.js (line 60)


//아이디 쀑볡체크
router.post('/idCheck',(req,res)=>{
    let id = req.body.idCheck
    let checkId = 'SELECT USER_ID FROM TB_USER WHERE USER_ID =?'
    console.log('아이디쀑볡',req.body.idCheck)
    let checkEnd = true
    conn.connect();
    conn.query(checkId,[id],(err,result)=>{
        if(err){
            console.log('쿼리문 μ—λŸ¬λ°œμƒ')
        }
        else{
            if(result.length == 0){
                console.log('νšŒμ›κ°€μž…κ°€λŠ₯')
            }
            else{
                console.log('이미 μ‘΄μž¬ν•˜λŠ” 아이디')
                checkEnd = false
            }
        }
        res.send(JSON.stringify(checkEnd))
    })
    
})

// νšŒμ›κ°€μž…
let checkEnd = false
router.post('/join',(req,res)=>{
    let id = req.body.userId // μ‚¬μš©μžκ°€ μž…λ ₯ν•œ ID
    let pw = req.body.userPw // μ‚¬μš©μžκ°€ μž…λ ₯ν•œ PW
    let name = req.body.userName // μ‚¬μš©μž 이름
    let nick = req.body.userNick // μ‚¬μš©μžκ°€ μž…λ ₯ν•œ NICKNAME
    let dev_year = req.body.dev_year // μ‹ μž…κ°œλ°œμž or κ²½λ ₯개발자
    let school = req.body.school // λ©˜ν†  or λ©˜ν‹°
    let langJs = req.body.js // = js
    let langPy = req.body.py // = py
    let insertInfo = 'INSERT INTO TB_USER ( USER_ID , USER_PW , USER_NAME , USER_NICK , USER_LEVEL , USER_MENTORING) VALUES (?,?,?,?,?,?)';
    let insertLang = 'INSERT INTO TB_LANG ( USER_ID , LANG_SKILL) VALUES (?,?)'
    

    conn.connect()
    conn.query(insertInfo,[id,pw,name,nick,dev_year,school],(err,result)=>{
        if(err){
            // console.log('μ—λŸ¬λ‚΄μš© :', err)
            console.log('이미 μ‘΄μž¬ν•˜λŠ” ID μž…λ‹ˆλ‹€.')
        }
        else{
            console.log('νšŒμ›κ°€μž…μ™„λ£Œ')
            if(langJs == 'javaScript'){
                conn.query(insertLang,[id,langJs],(err,result)=>{
                    if(err){
                        console.log('TB_LANG 쿼리문 였λ₯˜')
                    } 
                })
            }
            if(langPy == 'python'){
                conn.query(insertLang,[id,langPy],(err,result)=>{
                    if(err){
                        console.log('TB_LANG 쿼리문 였λ₯˜')
                    }
                })
            }
            
        }
        
    })
   
})

πŸ–ΌοΈ Preview

Join.gif

join

⚠️ **GitHub.com Fallback** ⚠️