Created
October 21, 2022 15:30
-
-
Save Mr-Malomz/84364d4fc27fd6b4fd6fc5640c5dc226 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package api | |
import ( | |
"context" | |
"go-sms-verification/data" | |
"net/http" | |
"time" | |
"github.com/gin-gonic/gin" | |
) | |
const appTimeout = time.Second * 10 | |
func (app *Config) sendSMS() gin.HandlerFunc { | |
return func(c *gin.Context) { | |
_, cancel := context.WithTimeout(context.Background(), appTimeout) | |
var payload data.OTPData | |
defer cancel() | |
app.validateBody(c, &payload) | |
newData := data.OTPData{ | |
PhoneNumber: payload.PhoneNumber, | |
} | |
_, err := app.twilioSendOTP(newData.PhoneNumber) | |
if err != nil { | |
app.errorJSON(c, err) | |
return | |
} | |
app.writeJSON(c, http.StatusAccepted, "OTP sent successfully") | |
} | |
} | |
func (app *Config) verifySMS() gin.HandlerFunc { | |
return func(c *gin.Context) { | |
_, cancel := context.WithTimeout(context.Background(), appTimeout) | |
var payload data.VerifyData | |
defer cancel() | |
app.validateBody(c, &payload) | |
newData := data.VerifyData{ | |
User: payload.User, | |
Code: payload.Code, | |
} | |
err := app.twilioVerifyOTP(newData.User.PhoneNumber, newData.Code) | |
if err != nil { | |
app.errorJSON(c, err) | |
return | |
} | |
app.writeJSON(c, http.StatusAccepted, "OTP verified successfully") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment